Implementing Lexical and Syntax Analysis with Lex and Yacc
Posted by Comunidad Wikiteka and classified in Computers
Written on in
English with a size of 2.73 KB
Lexer Implementation (lexer.l)
%{ #include "y.tab.h" %} %%% "+" { return PLUS; } "-" { return MINUS; } "*" { return MULTIPLY; } "/" { return DIVIDE; } "%" { return MODULUS; } "(" { return LPAREN; } ")" { return RPAREN; } [0-9]+ { yylval.num = atoi(yytext); return NUMBER; } [a-zA-Z][a-zA-Z0-9]* { yylval.id = strdup(yytext); return IDENTIFIER; } [ \t\n] { /* ignore whitespace */ } . { return yytext[0]; } %%%Parser Implementation (parser.y)
%{ #include <stdio.h> #include <stdlib.h> #include <string.h> void yyerror(const char *s); int yylex(); int temp_count = 0; int label_count = 0; void gen(char op, int arg1, int arg2, int result) { printf("%d: %c, %d, %d, %d\n", label_count++, op, arg1, arg2, result); } int new_temp() { return... Continue reading "Implementing Lexical and Syntax Analysis with Lex and Yacc" »