More LR Parsing and Bison CPSC 388 Ellen Walker Hiram College.

Slides:



Advertisements
Similar presentations
Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
Advertisements

Compiler Designs and Constructions
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
CSE 5317/4305 L4: Parsing #21 Parsing #2 Leonidas Fegaras.
Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Parser construction tools: YACC
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
Saumya Debray The University of Arizona Tucson, AZ 85721
LEX and YACC work as a team
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Introduction to Yacc Ying-Hung Jiang
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
More Parsing CPSC 388 Ellen Walker Hiram College.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
Three kinds of bottom-up LR parser SLR “Simple LR” –most restrictions on eligible grammars –built quite directly from items as just shown LR “Canonical.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
YACC Primer CS 671 January 29, CS 671 – Spring Yacc Yet Another Compiler Compiler Automatically constructs an LALR(1) parsing table from.
Parser Generation Tools (Yacc and Bison) CS 471 September 24, 2007.
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
COMPILER CONSTRUCTION
Syntax Analysis Part III
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
Chapter 4 Syntax Analysis.
Context-free Languages
Compiler design Bottom-up parsing: Canonical LR and LALR
Bottom-Up Syntax Analysis
Syntax Analysis Part III
Bison: Parser Generator
CMPE 152: Compiler Design December 5 Class Meeting
Simple, efficient;limitated
Syntax Analysis Sections :.
Syntax Analysis Part III
Parsing #2 Leonidas Fegaras.
Syntax Analysis Part III
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Compiler Construction
Subject: Language Processor
Parsing #2 Leonidas Fegaras.
Syntax Analysis - 3 Chapter 4.
Compiler Lecture Note, Miscellaneous
Yacc Yacc.
Compiler Structures 7. Yacc Objectives , Semester 2,
Compiler Design Yacc Example "Yet Another Compiler Compiler"
CMPE 152: Compiler Design December 4 Class Meeting
Review for the Midterm. Overview (Chapter 1):
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

More LR Parsing and Bison CPSC 388 Ellen Walker Hiram College

More than SLR(1) SLR(k) Parsing –Multiple-token lookahead (for shifts) and multiple-token follow information (for reductons) General LR(1) parsing –Include lookaheads in DFA construction LALR(1) parsing –Simplified state diagram for GLR(1) –What YACC / Bison uses

LALR: LR(0) + Lookahead NFA states are [ LR(0) item, lookahead] –Examples: [S->.(S), $], [S->.a,)] –After comma is first token after the RHS Building DFA –[S->.(S),$] --(--> [S->(.S),$] same as SLR –[S->(.S),$] --  --> [S->.(S),)] propagate LA Rule for every S-rule, every first of what follows S in original rule

YACC / Bison “Yet another Compiler-Compiler” Given CFG, automatically creates LALR table Using bison: –Input file: grammar.y –Output file: grammar.tab.c –Generic main reads lines, executes rules

Structure of a Bison File Definitions, including direct code in %{ %} % Rules of the grammar, with actions % Additional code, e.g. main(){ return yyparse() }

Example: Expression Calculator Rules describe the usual grammar –S’ -> exp –exp -> exp + term | exp - term | term –term -> term * factor | factor –factor -> NUMBER | ( exp ) Associated actions execute the arithmetic

Bison Rule Syntax LHS followed by : Each alternative followed by action, then | ; after the last action Example factor : NUMBER {$$ = $1;} | '(' exp ')' {$$ = $2;} ;

Bison Actions Rules include actions in { } (code) Predefined variables: –$$ value of result of rule (YYSTYPE or int) –$1 value of first token, $2 value of second token, etc. Example –Exp: exp ‘+’ term {$$ = $1 + $3;}

Bison and Flex together Define tokens in definition section: –%token ID –Choose values > 256 Make sure lex.yy.c and yy.tab.c agree on token ID defs #define ID val Compile both together –g++ -o myparser yy.tab.c lex.yy.c -lfl

Flex for Bison Each rule should return a token type –E.g. return NUMBER; In addition, a token value can be saved in the global variable yylval –E.g. yylval = myAtoI(yytext);

Mixing Characters and Tokens Don’t assign token values < 256 Allow characters to be their own tokens (rule):.return(yytext[0]); Or be specific: [-+*()]return(yytext[0]);

A Few Gotcha’s Bison (and flex) like tabs, not spaces Beware of commenting out closing } with // C (++) requires functions to be defined before they are used –Copy signatures to top of file –“extern” for functions and variables defined in other files

Bison Individual Homework Use Bison to parse and interpret simple LISP-like commands –( cons a (cons b nil)) => (a b) –( cons (cons a nil) (cons b nil) => ((a) b) –(car (cons a (cons b nil))) => a –(cdr (cons a (cons b nil))) => (b) –(cdr (cdr (cons a (cons b nil)))) => nil See handout for details

Error Handling in BU parsing Error = blank entry in parsing table To give specific error messages –Many error entries (but bigger table!) –Detect error before reducing when possible LR(1) is better than SLR(1) here

Recovery Panic mode: –Pop states from the stack until the parse can be restarted –Advance input until a legal transition is available Error productions –Treat “error” as a pseudotoken –Rules indicate how much to throw away

Error Example command : exp {cout << $1 << endl;} | error {yyerror “bad cmd”;} Once a command is in error, parser will –Perform the error action –Delete tokens until a legal follow of command ($ here)