Syntax Analysis Part III

Slides:



Advertisements
Similar presentations
 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.
Advertisements

Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
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.
Chapter 3 Chang Chi-Chung. The Structure of the Generated Analyzer lexeme Automaton simulator Transition Table Actions Lex compiler Lex Program lexemeBeginforward.
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
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.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
LEX (04CS1008) A tool widely used to specify lexical analyzers for a variety of languages We refer to the tool as Lex compiler, and to its input specification.
–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
Lex.
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.
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.
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.
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
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.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Yacc.
Language processing: introduction to compiler construction
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
Syntax-Directed Translation Part I
Simple, efficient;limitated
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax Analysis Part III
Yet Another Compiler Compiler
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Syntax-Directed Translation Part I
Syntax-Directed Translation
Yet Another Compiler Compiler
Compiler Lecture Note, Miscellaneous
Yacc Yacc.
Compiler Structures 7. Yacc Objectives , Semester 2,
Appendix B.2 Yacc Appendix B.2 -- Yacc.
Saumya Debray The University of Arizona Tucson, AZ 85721
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Syntax-Directed Translation Part I
CMPE 152: Compiler Design December 4 Class Meeting
Systems Programming & Operating Systems Unit – III
Presentation transcript:

Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011

ANTLR, Yacc, and Bison ANTLR tool Yacc (Yet Another Compiler Compiler) Generates LL(k) parsers Yacc (Yet Another Compiler Compiler) Generates LALR(1) parsers Bison Improved version of Yacc

Creating an LALR(1) Parser with Yacc/Bison yacc specification yacc.y Yacc or Bison compiler y.tab.c C compiler y.tab.c a.out a.out input stream output stream

Yacc Specification A yacc specification consists of three parts: yacc declarations, and C declarations within %{ %} %% translation rules %% user-defined auxiliary procedures The translation rules are productions with actions: production1 { semantic action1 } production2 { semantic action2 } … productionn { semantic actionn }

Writing a Grammar in Yacc Productions in Yacc are of the form Nonterminal : tokens/nonterminals { action } | tokens/nonterminals { action } … ; Tokens that are single characters can be used directly within productions, e.g. ‘+’ Named tokens must be declared first in the declaration part using %token TokenName

Synthesized Attributes Semantic actions may refer to values of the synthesized attributes of terminals and nonterminals in a production: X : Y1 Y2 Y3 … Yn { action } $$ refers to the value of the attribute of X $i refers to the value of the attribute of Yi For example factor : ‘(’ expr ‘)’ { $$=$2; } factor.val=x $$=$2 ( ) expr.val=x

Example 1 Also results in definition of #define DIGIT xxx %{ #include <ctype.h> %} %token DIGIT %% line : expr ‘\n’ { printf(“%d\n”, $1); } ; expr : expr ‘+’ term { $$ = $1 + $3; } | term { $$ = $1; } ; term : term ‘*’ factor { $$ = $1 * $3; } | factor { $$ = $1; } ; factor : ‘(’ expr ‘)’ { $$ = $2; } | DIGIT { $$ = $1; } ; %% int yylex() { int c = getchar(); if (isdigit(c)) { yylval = c-’0’; return DIGIT; } return c; } Attribute of factor (child) Attribute of term (parent) Attribute of token (stored in yylval) Example of a very crude lexical analyzer invoked by the parser

Dealing With Ambiguous Grammars By defining operator precedence levels and left/right associativity of the operators, we can specify ambiguous grammars in Yacc, such as E  E+E | E-E | E*E | E/E | (E) | -E | num To define precedence levels and associativity in Yacc’s declaration part: %left ‘+’ ‘-’ %left ‘*’ ‘/’ %right UMINUS

Example 2 Double type for attributes and yylval %{ #include <ctype.h> #include <stdio.h> #define YYSTYPE double %} %token NUMBER %left ‘+’ ‘-’ %left ‘*’ ‘/’ %right UMINUS %% lines : lines expr ‘\n’ { printf(“%g\n”, $2); } | lines ‘\n’ | /* empty */ ; expr : expr ‘+’ expr { $$ = $1 + $3; } | expr ‘-’ expr { $$ = $1 - $3; } | expr ‘*’ expr { $$ = $1 * $3; } | expr ‘/’ expr { $$ = $1 / $3; } | ‘(’ expr ‘)’ { $$ = $2; } | ‘-’ expr %prec UMINUS { $$ = -$2; } | NUMBER ; %% Double type for attributes and yylval

Example 2 (cont’d) %% int yylex() { int c; while ((c = getchar()) == ‘ ‘) ; if ((c == ‘.’) || isdigit(c)) { ungetc(c, stdin); scanf(“%lf”, &yylval); return NUMBER; } return c; } int main() { if (yyparse() != 0) fprintf(stderr, “Abnormal exit\n”); return 0; } int yyerror(char *s) { fprintf(stderr, “Error: %s\n”, s); } Crude lexical analyzer for fp doubles and arithmetic operators Run the parser Invoked by parser to report parse errors

Combining Lex/Flex with Yacc/Bison yacc specification yacc.y Yacc or Bison compiler y.tab.c y.tab.h Lex specification lex.l and token definitions y.tab.h Lex or Flex compiler lex.yy.c C compiler lex.yy.c y.tab.c a.out a.out input stream output stream

Lex Specification for Example 2 %option noyywrap %{ #include “y.tab.h” extern double yylval; %} number [0-9]+\.?|[0-9]*\.[0-9]+ %% [ ] { /* skip blanks */ } {number} { sscanf(yytext, “%lf”, &yylval); return NUMBER; } \n|. { return yytext[0]; } Generated by Yacc, contains #define NUMBER xxx Defined in y.tab.c yacc -d example2.y lex example2.l gcc y.tab.c lex.yy.c ./a.out bison -d -y example2.y flex example2.l gcc y.tab.c lex.yy.c ./a.out

Error production: set error mode and skip input until newline Error Recovery in Yacc %{ … %} … %% lines : lines expr ‘\n’ { printf(“%g\n”, $2; } | lines ‘\n’ | /* empty */ | error ‘\n’ { yyerror(“reenter last line: ”); yyerrok; } ; … Error production: set error mode and skip input until newline Reset parser to normal mode