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.

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
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
Context-Free Grammars Lecture 7
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
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
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.
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
Compiler Tools Lex/Yacc – Flex & Bison. Compiler Front End (from Engineering a Compiler) Scanner (Lexical Analyzer) Maps stream of characters into words.
–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.
Syntactic Analysis Tools
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.
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.
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.
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.
Semantic Values and Symbol Tables © Allan C. Milne Abertay University v
LECTURE 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
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.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
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.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Yacc.
Syntax Analysis Part III
Tutorial On Lex & Yacc.
Compiler Construction
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Chapter 4 Syntax Analysis.
Context-free Languages
Syntax Analysis Part III
Bison: Parser Generator
TDDD55- Compilers and Interpreters Lesson 3
Syntax Analysis Part III
Syntax Analysis Part III
Syntax Analysis Part III
Syntax Analysis - 3 Chapter 4.
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"
Systems Programming & Operating Systems Unit – III
Presentation transcript:

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 specifying valid structures with a context-free grammar, we can attach meaning to the structures with an attribute grammar. Attribute grammars contain productions, as in a context-free grammar, as well as semantic actions to perform when the production is taken. production semantic rule  A.a :=...; B.a :=...; C.a :=...

REVIEW OF LAST LECTURE We decorate a parse tree by evaluating the attributes of the nodes. The attributes may have a complicated dependency hierarchy. An attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used). Some attribute grammars, such as S-attributed and L-attributed grammars, give rise to straightforward attribute flow algorithms (such as one depth-first traversal of the tree).

ATTRIBUTE GRAMMAR EXAMPLES

Here’s another example grammar. Let’s write an attribute grammar that converts the binary number to a decimal number and prints it out.

ATTRIBUTE GRAMMAR EXAMPLES Let’s write an attribute grammar that converts the binary number to a decimal number and prints it out.

ATTRIBUTE GRAMMAR EXAMPLES Let’s write an attribute grammar that converts the binary number to a decimal number and prints it out.

ATTRIBUTE GRAMMAR EXAMPLES Let’s write an attribute grammar that converts the binary number to a decimal number and prints it out.

ATTRIBUTE GRAMMAR EXAMPLES Let’s write an attribute grammar that converts the binary number to a decimal number and prints it out.

ATTRIBUTE GRAMMAR EXAMPLES Let’s write an attribute grammar that converts the binary number to a decimal number and prints it out.

INTRODUCTION TO YACC Yacc (Yet Another Compiler Compiler) Automatically generate an LALR (Look-ahead LR) parser for a context free grammar. Parses a subset of LR grammars. Can parse LL(1) grammars as long as symbols with empty derivations also have non- empty derivations. Allows syntax-directed translation by writing grammar productions and semantic actions. Works with Lex by calling yylex() to get the next token.

YACC Yacc file format: declarations /* specify tokens, non-terminals, and other C constructs */ % translation rules /* specify grammar here */ % supporting C-routines The command “yacc yaccfile” produces y.tab.c, which contains a routine yyparse(). yyparse() calls yylex() to get tokens. yyparse() returns 0 if the program is grammatically correct, non-zero otherwise.

YACC DECLARATIONS %{ #include #include %} %token NUMBER NAME % % Include some necessary libraries. Specify the tokens we will be using.

YACC TRANSLATION RULES statement:NAME '=' expression | expression ; expression: expression '+' NUMBER | expression '-' NUMBER | NUMBER ; S  name = E S  E E  E + num E  E – num E  num

YACC ATTRIBUTE GRAMMARS Each symbol can be associated with some attributes and semantic actions can be associated with productions. item : LPAREN exp RPAREN {$$ = $2;} | NUMBER {$$ = $1;} ; $$ is the attribute associated with the left-hand side of the production. $1 is the attribute associated with the first symbol in the right-hand side, $2 for the second symbol, …

YACC ENVIRONMENT Yacc processes a yacc specification file and produces a y.tab.c file. An integer function yyparse() is produced by Yacc. Calls yylex() to get tokens. Return non-zero when an error is found, or 0 if the program is accepted. Need main() and and yyerror() functions. yyerror(const char *str){ printf("yyerror: %s at line %d\n", str, yyline); } int main(){ return yyparse(); }

LEX AND YACC TOGETHER %{ #include “y.tab.h" extern int yylval; %} % [0-9]+{ yylval = atoi (yytext); printf ("scanned the number %d\n", yylval); return NUMBER; } [ \t] { printf ("skipped whitespace\n"); } \n { printf ("reached end of line\n"); return 0;}. { printf ("found other data \"%s\"\n", yytext); return yytext[0];} % ex.l yylval accessible by Yacc as image of token. Include the header file for the parser. What kinds of tokens does this lex file identify?

LEX AND YACC TOGETHER %{ #include “y.tab.h" extern int yylval; %} % [0-9]+{ yylval = atoi (yytext); printf ("scanned the number %d\n", yylval); return NUMBER; } [ \t] { printf ("skipped whitespace\n"); } \n { printf ("reached end of line\n"); return 0;}. { printf ("found other data \"%s\"\n", yytext); return yytext[0];} % ex.l yylval accessible by Yacc as image of token (note that it is of type int here). Include the header file for the parser. What kinds of tokens does this lex file identify? - Integers -Whitespace -Newlines -Single characters that do not fall into any other category

LEX AND YACC TOGETHER %{ #include %} %token NAME NUMBER % statement: NAME '=' expression { printf("pretending to assign %s the value %d\n", $1, $3); } | expression { printf("= %d\n", $1); } ; expression: expression '+' NUMBER { $$ = $1 + $3; printf ("Recognized '+' expression.\n");} | expression '-' NUMBER { $$ = $1 - $3; printf ("Recognized '-' expression.\n");} | NUMBER { $$ = $1; printf ("Recognized a number.\n");} ; % int main (void) { return yyparse();} int yyerror (char *msg) { return fprintf (stderr, "YACC: %s\n", msg); } int yywrap(){ return 1; }

LEX AND YACC TOGETHER ex.l -d ex.y lex.yy.c y.tab.c < test.txt scanned the number 3 Recognized a number. skipped whitespace found other data "+" skipped whitespace scanned the number 4 Recognized '+' expression. reached end of line = test.txt yacc –d: cause the header file y.tab.h to be written.

LEX AND YACC TOGETHER %{ #include “y.tab.h" extern int yylval; %} % [0-9]+{ yylval = atoi (yytext); printf ("scanned the number %d\n", yylval); return NUMBER; } [ \t] { printf ("skipped whitespace\n"); } \n { printf ("reached end of line\n"); return 0;}. { printf ("found other data \"%s\"\n", yytext); return yytext[0];} % ex.l Notice that yylval is an int here. What about when we want the image of strings?

LEX AND YACC TOGETHER Let’s make yylval a union so that it can represent multiple types. In our yacc file: %{ #include %} %union { int number; char *string; } %token NAME %token NUMBER %type expression % Makes yylval a union of int and char* Now, we specify the type we need to use from the union for not only the tokens but also the non-terminals which are used in semantic actions.

LEX AND YACC TOGETHER In our lex file: %{ #include "y.tab.h" %} % [a-zA-Z]+ {yylval.string=yytext; printf(“scanned the name %s\n”, yylval); return NAME;} [0-9]+ { yylval.number = atoi (yytext); printf ("scanned the number %d\n", yylval); return NUMBER; } [ \t] { printf ("skipped whitespace\n"); } …. Now, we must specify the type as an attribute of yylval. Also, we added rules for picking up the NAME token.

LEX AND YACC TOGETHER ex.l -d ex.y lex.yy.c y.tab.c < test.txt scanned the number a skipped whitespace found other data "=" skipped whitespace scanned the number 3 Recognized a number. skipped whitespace found other data "+" skipped whitespace scanned the number 4 Recognized '+' expression. reached end of line pretending to assign a = the value 7

MULTIPLE ATTRIBUTES It is possible to define multiple attributes for a symbol by adding structs to your union. For example, let’s say you want a symbol to be associated with both a type and id. %union{ struct{ int type; char *id; } type_id; } %type var

CONFLICTS Keep in mind that Yacc produces an LALR parser. If the grammar is not LALR, there may be conflicts that prevent parsing. Yacc will try to resolve them automatically, but some conflicts will need your intervention. Where it is necessary, specify precedence and associativity of operators: use keywords %left, %right, %nonassoc in the declarations section. - All tokens on the same line are the same precedence level and associativity. - The lines are listed in order of increasing precedence. %left PLUSnumber, MINUSnumber %left TIMESnumber, DIVIDEnumber