CPSC 325 - Compiler Tutorial 5 Parser & Bison. Bison Concept Bison reads tokens and pushes them onto a stack along with the semantic values. The process.

Slides:



Advertisements
Similar presentations
Compiler Construction Sohail Aslam Lecture StackInput ¤0¤0 id – id  id $ s4 ¤0 id 4 – id  id $ r6 F → id ¤0F3¤0F3 – id  id $ r5 T → F ¤0T2¤0T2.
Advertisements

Chapter 3 Syntax Analysis
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
YANGYANG 1 Chap 5 LL(1) Parsing LL(1) left-to-right scanning leftmost derivation 1-token lookahead parser generator: Parsing becomes the easiest! Modifying.
CSE 5317/4305 L4: Parsing #21 Parsing #2 Leonidas Fegaras.
Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Language processing: introduction to compiler construction Andy D. Pimentel Computer Systems Architecture group
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
ML-YACC David Walker COS 320. Outline Last Week –Introduction to Lexing, CFGs, and Parsing Today: –More parsing: automatic parser generation via ML-Yacc.
CS 310 – Fall 2006 Pacific University CS310 Lex & Yacc Today’s reference: UNIX Programming Tools: lex & yacc by: Levine, Mason, Brown Chapter 1, 2, 3 November.
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
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
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
LEX and YACC work as a team
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
Syntax and Backus Naur Form
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.
1 Programming Languages (CS 550) Lecture 1 Summary Grammars and Parsing Jeremy R. Johnson.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
CPS 506 Comparative Programming Languages Syntax Specification.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Syntactic Analysis Operator-Precedence Parsing Recursive-Descent Parsing.
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.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
Introduction to YACC CS 540 George Mason University.
1 Programming Languages (CS 550) Lecture 1 Summary Grammars and Parsing Jeremy R. Johnson.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Project Part 2: Parser. Command line: bison –d translate.y Command line: flex filename.l gcc y.tab.c lex.yy.c -lfl Bison.
YACC Primer CS 671 January 29, CS 671 – Spring Yacc Yet Another Compiler Compiler Automatically constructs an LALR(1) parsing table from.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
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,
Introduction to Parsing
Yacc.
Language processing: introduction to compiler construction
CS510 Compiler Lecture 4.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Context-free Languages
Bison: Parser Generator
Bison Marcin Zubrowski.
Parsing #2 Leonidas Fegaras.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Parsing #2 Leonidas Fegaras.
5. Bottom-Up Parsing Chih-Hung Wang
Kanat Bolazar February 16, 2010
Compiler Lecture Note, Miscellaneous
Compiler Structures 7. Yacc Objectives , Semester 2,
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Saumya Debray The University of Arizona Tucson, AZ 85721
Compiler Design Yacc Example "Yet Another Compiler Compiler"
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

CPSC Compiler Tutorial 5 Parser & Bison

Bison Concept Bison reads tokens and pushes them onto a stack along with the semantic values. The process of pushing a token onto stack is called “shifting”. Example: ‘10 – 3 *’ with a 2 come. There are four tokens in stack. (shifted four tokens). Reduction is when the last n tokens match the components of a grammar rules, and they can be combined according to the rules. Example: 10 – 3 * 2  10 – 6  4

Look-Ahead Token Bison parser does NOT always reduce immediately as soon as the last n tokens and grouping match a rule. The parser sometimes “look ahead” at the next token in order to decide what to do.

Look-Ahead Tokens (cont) When a token is read, it become look-ahead token first, and which is not on the stack. Then the parser do one or more reductions of tokens and grouping on the stack. When no more reductions should take place, the look-ahead token is shifted onto the stack.

Example expr: term ‘–’ expr | term term: ‘(‘ expr ‘)’ | term ‘!’ | NUMBER Example: ‘3 – 5’ had been read and shift; what should be done? Depends on next token. 1. ‘)’: then reduce ‘3 – 5’ to ‘expr’; there is no rule for term ’)’ 2. ‘!’: then it must be shifted immediately, so that ‘5 !’ can be reduced to make a ‘term’. There is no rule for expr ‘!’

Conflicts If_stmt: IF expr THEN stmt | IF expr THEN stmt ELSE stmt Bison choose to “shift” the ELSE instead of “reduce” in this case. (Match the innermost if-statement) The grammar is ambiguous

Sample if-else in Bison %token IF THEN ELSE variable % stmt: expr | if_stmt If_stmt: IF expr THEN stmt | IF expr THEN stmt ELSE stmt expr: variable

Precedence expr: expr ‘+’ expr | expr ‘*’ expr | expr ‘<‘ expr | ‘(‘ expr ‘)’ | … We have input ‘3 + 2’ 1. ‘)’ – no problem, just reduce to expr 2. ‘*’ or ‘<‘ – Oh! Oh! problem….. What should we do?

Solution Break them apart as mention before. Bison declaration – ‘1 + (2 op 3)’ or (1 + 2) op 3 will lead to different result sometimes. – Example: ‘1 – 2 – 3’ should be (1 – 2) – 3 or 1 – (2 – 3)? We want the first one, which is left association. The second case is right association, which is desirable for assignment operators. %nonassoc is for error catching – In our case, we will have the following declaration %left ‘ ’ ‘=‘ NE LE GE %left ‘+’ ‘-’ %left ‘*’ ‘/’

Context-Dependent Precedence ‘–’ has very low precedence when it acts as an ‘subtract’. Nevertheless, when it acts as an unary ‘Negative’ operator. In Bison, each token can only been declare once in %left, %right and %nonassoc. Therefore, for context-dependent precedence, we need to use an additional mechanism: the %prec modifier for rules.

Sample code for Unary minus … %left ‘+’ ‘-’ %left ‘*’ ‘/’ %left UMINUS exp: … | exp ‘-’ exp … | ‘-’ exp %prec UMINUS …

yyparse() Implemented using a finite-state machine. Each time a look-ahead token is read, it will either take the action of “Shift the look-ahead token” or “Reduce using rule number n”.

Reduce Conflicts Consider the following grammars sequence: /*empty*/ { printf(“empty sequence\n”);} | maybeword | sequence word { printf(“added word %s\n, $2);} maybeword: /*empty*/ {printf(“empty maybeword\n”);} | word { printf(“single word %s\n, $1); }

Solution Bison resolves a reduce conflict by choosing to use the rule that appears first in the grammar, but it is very risky to rely on this. sequence: /*empty*/ { printf(“empty sequence\n”);} | sequence word { printf (“added word %s\n, $2); }

Left recursive sequence: /*empty*/ | sequence words | sequence redirects words: /*empty*/ | words word redirects: /*empty*/ | redirects redirect

Bison Structure %{ C declarations %} Yacc/Bison declarations % Grammar rules % Additional C code

Compile Bison file bash$ flex calc.l  generate lex.yy.c bash$ bison -d calc.y  generate calc.tab.c and calc.tab.h bash$ gcc –o calc *.c –ll –lm bash$ calc