LEX-AND-YACC

About Lex

This project is maintained by ckshitij

LEX and YACC

How to Compile code

Lex Program Structure

A lex program has the following basic structure:

%{
   C declarations and includes
%}
   Lex macro definitions and directives
%%
   Lex Specification
   in the form of pattern/action statements like this:
   keyword    { my_c_code(yytext); }
%%
   C language program (the rest)

However, the only mandatory part is the first %%

Lex generates yylex()

main() 
...which simply calls yylex()
yywrap() 
...which always returns non-zero.
%{
#include <stdio.h>
#include <errno.h>
int file_num;
int file_num_max;
char **files;
extern int errno;
%}
%%
(ftp|http):\/\/[^ \n<>"]*	printf("%s\n",yytext);
.|\n			;
%%
int main(int argc, char *argv[]) {
	file_num=1;
	file_num_max = argc;
	files = argv;
	if ( argc > 1 ) {
		if ( (yyin = fopen(argv[file_num],"r")) == 0 ) {
			perror(argv[file_num]);
			exit(1);
		}
	}
	while( yylex() )
		;
	return 0;
}

int yywrap() {
	fclose(yyin);
	if ( ++file_num < file_num_max ) {
		if ( (yyin = fopen(files[file_num],"r")) == 0 ) {
			perror(files[file_num]);
			exit(1);
		}
		return 0;
	} else {
		return 1;
	}
}

yylex() and return()

YACC - ( Yet Another Compiler-Compiler or A parser generator )

Yacc Program Structure

Yacc Grammar Rules

Simple Rule

Yacc Actions

Token Types

Now we need to modify the lexer. This is done by including the line:

yylval.str = strdup(yytext); just before the lexer returns the LABEL and EXEC tokens. We'll also include the line:

yylval.num = atoi(yytext); just before the lexer returns the __INT__ token. ```

Our Prototype Parser

The yyerror() function

The yyerror() function is called when yacc encounters an invalid syntax. The yyerror() is passed a single string (char) argument. Unfortunately, this string usually just says “parse error”, so on its own, it’s pretty useless. Error recovery is an important topic which we will cover in more detail later on. For now, we just want a basic yyerror() function like this:

yyerror(char *err) {
	fprintf(stderr, "%s\n",err);
}
See the section "Interface" in the Bison documentation for more information.

References