{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

02-04shiftReduceParsing - Yacc/Bison Overview Parsing and...

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

View Full Document Right Arrow Icon
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(); … }
Image of page 1

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

View Full Document Right Arrow Icon
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.cc – Historical relic: “tab” short for “table”
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern