1 / 10

SET 5a

SET 5a. Standard parts of the input files for Lex and Yacc. The parts in red in the slides following are the standard parts which should be included in every use of Lex combined with Yacc. Generating the Parser Using YACC. The structure of a grammar to be used with

kaemon
Download Presentation

SET 5a

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SET 5a Standard parts of the input files for Lex and Yacc

  2. The parts in red in the slides following are the standard parts which should be included in every use of Lex combined with Yacc.

  3. Generating the Parser Using YACC • The structure of a grammar to be used with • YACC for generating a parser is similar to • that of LEX.  There is a definition section, • a rules (productions) section, and a code • section.

  4. Example of an Input Grammar for YACC %{ /* ARITH.text Yacc input for a arithmetic expression evaluator The text in red will occur as shown in every Yacc input file */ #include <stdio.h> /* for printf */ #define YYSTYPE int int yyparse(void); int yylex(void); void yyerror(char *mes); %} %token number %%

  5. Example of an Input Grammar for YACC (Cont.1) program : expression {printf("answer = %d\n", $1);} ; expression : expression '+' term {$$ = $1 + $3;} | term ; term : term '*' number {$$ = $1 * $3;} | number ; %%

  6. Example of an Input Grammar for YACC (Cont.2) void main() { printf("Enter an arithmetic expression\n"); yyparse();} /* prints an error message */ void yyerror(char *mes) {printf("%s\n", mes);} NOTE. It is never necessary to employ $$ = $1 since that is the default.

  7. The LEX scanner definition file for the arithmetic expressions grammar %{ /* lexarith.text lex input for a arithmetic expression evaluator */ #include “y.tab.h" /* may be called something else in non-Unix systems*/ #include <stdlib.h> /* for atoi */ #define YYSTYPE int extern YYSTYPE yylval; %} digit [0-9] %% {digit}+ {yylval = atoi(yytext); return number; } (" "|\t)* ; \n {return(0);} /* recognize Enter key as EOF */ . {return yytext[0];} %% int yywrap() {}

  8. To produce a compiler from the above files lexarith.text and arith.text, employ the following (in Cygwin): flex lexarith.text yacc –d arith.text gcc y.tab.c lex.yy.c If the source to be compiled is source.text, then employ ./a < source.text

  9. The use of the –d option in yacc –d ...creates y.tab.h. In Cygwin, this file contains default definitions that should be erased. Erase from the line starting with #if ! defined to the end. Unless you change the set of terminals in your grammar, omit the –d option is subsequent uses of Yacc, and so avoid needing to alter y.tab.h again.

  10. Note 2. For those unfamiliar with C. In Cygwin gcc produces a.exe. In other Unix systems, it usually produces a.out, in which the compile step would be ./a.out < source.text To get gcc to produce an output with another name e.g. myprog, employ: gcc –o myprog y.tab.c lex.yy.c

More Related