Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.

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

Structure of a YACC File Has the same three-part structure as Lex Each part is separated by a % symbol The three parts are even identical: – definition.
Chapter 3 Syntax Analysis
 Lex helps to specify lexical analyzers by specifying regular expression  i/p notation for lex tool is lex language and the tool itself is refered to.
Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
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
Introduction To Yacc and Semantics © Allan C. Milne Abertay University v
1 Using Yacc: Part II. 2 Main() ? How do I activate the parser generated by yacc in the main() –See mglyac.y.
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
1 October 14, October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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 컴파일러 입문.
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
–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
Introduction to YACC Panfeng Xue
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
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.
Lecture 7. YACC YACC can parse input streams consisting of tokens with certain values. This clearly describes the relation YACC has with Lex, YACC has.
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.
ICS312 LEX Set 25. LEX Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the C program.
Applications of Context-Free Grammars (CFG) Parsers. The YACC Parser-Generator. by: Saleh Al-shomrani.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
Introduction to YACC CS 540 George Mason University.
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 LR Parsing and Bison CPSC 388 Ellen Walker Hiram College.
LECTURE 11 Semantic Analysis and Yacc. REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely.
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.
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.
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,
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LEX & Yacc Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
COMPILER CONSTRUCTION
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Yacc.
COMPILER CONSTRUCTION
Syntax Analysis Part III
Tutorial On Lex & Yacc.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Chapter 4 Syntax Analysis.
Context-free Languages
Syntax Analysis Part III
Bison: Parser Generator
Simple, efficient;limitated
Syntax Analysis Part III
Bison Marcin Zubrowski.
Syntax Analysis Part III
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Compiler Lecture Note, Miscellaneous
Yacc Yacc.
Compiler Structures 7. Yacc Objectives , Semester 2,
Appendix B.2 Yacc Appendix B.2 -- Yacc.
Compiler Design Yacc Example "Yet Another Compiler Compiler"
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014

5. Bottom-Up Parsing PART THREE

Contents PART ONE 5.1 Overview of Bottom-Up Parsing 5.2 Finite Automata of LR(0) Items and LR(0) Parsing PART TWO 5.3 SLR(1) Parsing 5.4 General LR(1) and LALR(1) Parsing 5.7 Error Recovery in Bottom-Up Parsers Part THREE 5.5 Yacc: An LALR(1) Parser Generator

5.5 Yacc: LALR(1) PARSING GENERATOR

 A parser generator is a program taking a specification of the syntax of a language and producing a parser procedure for the language  A parser generator is called compiler- compiler  One widely used parser generator incorporating the LALR(1) parsing algorithm is called YACC

5.5.1 Yacc Basics

Parser generator--Yacc Yacc Yacc source file translate.y y.tab.c Ccompiler y.tab.c y.tab.c a.out a.out input output Lex Lex source File x.l lex.yy.c Ccompiler lex.yy.c a.out a.out inputStream Tokens 1/36

 Yacc takes a specification file  (usually with a.y suffix);  Yacc produces an output file consisting of C source code for the parser  (usually in a file called y.tab.c or ytab.c or, more recently.tab.c, where.y is the input file).  A Yacc specification file has the basic format.  {definitions}  %  {rules}  %  {auxiliary routines}  There are three sections separated by lines containing double percent signs

 The example:  A calculator for simple integer arithmetic expressions with the grammar exp → exp addop term | termx addop → + | - term → term mulop factor | factor mulop → * factor → ( exp ) | number

%{ #include %} %token NUMBER % command :exp { printf ( “ %d\n ”,$1);} ; /*allows printing of the result */ exp: exp ‘ + ’ term {$$ = $1 + $3;} | exp ‘ - ‘ term {$$ = $1 - $3;} | term {$$ = $1;} ;

term: term ‘ * ’ factor {$$ = $1* $3;} | factor {$$ = $1;} ; factor :NUMBER{$$ = $1;} | ‘ ( ‘ exp ’ ) ’ {$$=$2;} ; % main ( ) { return yyparse( ); }

int yylex(void) { int c; while( ( c = getchar ( ) )== ‘ ’ ); /*eliminates blanks */ if ( isdigit(c) ) { unget (c,stidin) ; scanf ( “ %d ”,&yylval ) ; return (NUMBER ) ; } if (c== ‘ \n ’ ) return 0; /* makes the parse stop */ return ( c ) ; }

int yyerror (char * s) { fprintf (stderr, “ %s\n ”,s ) ; return 0; }/* allows for printing of an error message */

 The definitions section (can be empty)  Information about the tokens, data types, grammar rules;  Any C code that must go directly into the output file at its beginning  The rules section  Grammar rules in a modified BNF form;  Actions in C code executed whenever the associated grammar rule is recognized  (i.e.. used in a reduction. according to the LALR(1) parsing algorithm)  The metasymbol conventions:  The vertical bar is used for alternatives;  The arrow symbol →is replaced in Yacc by a colon;  A semicolon must end each grammar rule.

 The auxiliary routines section ( can be empty):  Procedure and function declarations  A minimal Yacc specification file  consist only of % followed by grammar rules and actions.

Notes:  Two typical #include directives are set off from other Yacc declarations in the definitions section by the surrounding delimiters %{ and %}.  Yacc has two ways of recognizing token.  Single-character inside single quotes as itself  Symbolic tokens as a numeric value that does not conflict with any character value

Notes:  %start command in the definition section will indicate that command be start symbol, otherwise the rule listed first in the rule section indicates the start symbol.  The variable yylval is defined internally by Yacc;  yyparse is the name of the parsing procedure;  yylex is the name of Lex scanner generator.

5.5.2 Yacc Options

(1) – d option  It will produce the header file usually named y.tab.h or ytab.h.  Yacc needs access to many auxiliary procedures in addition to yylax and yyerror; They are often placed into external files rather than directly in the Yacc specification file.  Making Yacc-specific definitions available to other files.  For example, command in the file calc.y  yacc – d calc.y

(1) – d option Produce (in addition to the y.tab.c file) the file y.tab.h (or similar name), whose contents vary, but typically include such things as the following: #ifndef YYSTYPE #define YYSTYPE int #endif #define NUMBER 258 extern YYSTYPE yylval;

(2) – v option  Produces yet another file, with the name y. output (or similar name).  This file contains a textual description of the LALR( l )parsing table that is used by the parser.  As an example: consider the skeletal version of the Yacc specification above.  yacc -v calc.y

(2) – v option %taken NUMBER % command :exp ; exp: exp ‘ + ’ term | exp ‘ - ‘ term | term ; term: term ‘ * ’ factor | factor ; factor :NUMBER | ‘ ( ‘ exp ’ ) ’ ;

5.5.3 Parsing Conflicts and Disambiguating Rules

Yacc and ambiguous rules Default rules for Disambiguating :  reduce-reduce conflict: preferring the reduction by the grammar rule listed first in the specification file  shift -reduce conflict: preferring the shift over the reduce

Error recovery in Yacc The designer of a compiler will decide:  Which non-terminals may cause an error  Add error production A  error  for the non-terminals  Add semantic actions for these error productions Yacc regards the error productions as normal grammar productions

Error recovery procedure  Pop the state from stack , until we find A  ·error   Push ‘ error’ into stack  ,  If  is  , do the reduction , and scan the input symbols until we find the correct symbol.  ,  If  is not  , find  , push  into stack and use A  ·error  to do the reduction. s stack a.. A find an error s : C   1 ·A  2 A  · b  A  ·error ... C  1 A·  2... A A  b· ... b

lines: lines expr ‘\n’{printf ( “%g \n”, $2 ) } | lines ‘\n’ | /    / | error ‘\n’{printf ( “ 重新输入上一行 ”); yyerrok;} ;

End of Part Three THANKS