02-04shiftReduceParsing

02-04shiftReduceParsing - 1 CSE 450: Compilers K. Stirewalt...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSE 450: Compilers K. Stirewalt Parsing and syntax analysis Part 4: Bottom-up parsing Topics: Quick introduction to yacc/bison Shift-reduce parsing Handles and handle pruning CSE 450: Compilers K. Stirewalt Yacc/Bison Overview declarations %% rules %% support code Format of .y file (same structure as lex file) foo.y foo.tab.c a.out g++ bison yyparse() is entry into generated parser CSE 450: Compilers K. Stirewalt Declarations Section User types: As in lex, these are in a section bracketed by %{ and %} Tokens terminal symbols of the grammar %token terminal1 terminal2 ... Values for tokens assigned sequentially after all ASCII characters. Use -d option in bison to generate foo.tab.h to include token definitions in your flex file. Other commands are possible, such as %left and %right to control associativity of tokens and %prec to control precedence. CSE 450: Compilers K. Stirewalt Rules Section Every name not previously declared is assumed to be a non-terminal Productions non-terminal: first_prod | second_prod | ... ; production has the form: non-terminal : ; Thus you can say: foo: production | /* nothing*/ ; CSE 450: Compilers K. Stirewalt Lex + Yacc Designed to work together Function yyparse makes calls to function yylex to get tokens Yacc/bison provide options for generating token symbols for use in lex/flex files Thus, no need to #define these symbols in the lex file Variables, such as yylval , can be used to pass information from lexer to parser CSE 450: Compilers K. Stirewalt Dependencies in lex/yacc files Lex file fooLexer.l: %{ #include fooParser.h %} %% regular expressions/actions %% Yacc file fooParser.y: %{ extern int yylex(); void yyerror(const char*); %} token declarations %% productions %% void yyerror(const char* s) {} int main(void) { yyparse(); } 2 CSE 450: Compilers K. Stirewalt Tips when building yacc parsers Watch for conflicts in your grammar will be reported by yacc the file y.output will list information that helps in resolving these conflicts Yacc-generated files are always called y.tab.h and y.tab.ccand y....
View Full Document

This note was uploaded on 07/25/2008 for the course CSE 450 taught by Professor Stirewalt during the Spring '08 term at Michigan State University.

Page1 / 5

02-04shiftReduceParsing - 1 CSE 450: Compilers K. Stirewalt...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online