Compiler Design Yacc Example "Yet Another Compiler Compiler"

Slides:



Advertisements
Similar presentations
Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
Advertisements

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.
Tools for building compilers Clara Benac Earle. Tools to help building a compiler C –Lexical Analyzer generators: Lex, flex, –Syntax Analyzer generator:
Parser construction tools: YACC
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
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 컴파일러 입문.
Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad.
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
CPS 506 Comparative Programming Languages Syntax Specification.
Syntax Specification with YACC © Allan C. Milne Abertay University v
Introduction to Lex Ying-Hung Jiang
–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
Practical 1-LEX Implementation
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
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.
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.
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.
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.
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.
Java CUP.
Compiler Construction
Chapter 4 Syntax Analysis.
Context-free Languages
CS 153: Concepts of Compiler Design December 5 Class Meeting
TDDD55- Compilers and Interpreters Lesson 2
Syntax Analysis Part III
Bison: Parser Generator
CMPE 152: Compiler Design December 5 Class Meeting
TDDD55- Compilers and Interpreters Lesson 3
Simple, efficient;limitated
Compiler Design 4. Language Grammars
Syntax Analysis Part III
Bison Marcin Zubrowski.
Syntax Analysis Part III
Yet Another Compiler Compiler
Review: Compiler Phases:
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Subject: Language Processor
Syntax Analysis - 3 Chapter 4.
Yet Another Compiler Compiler
Compiler Lecture Note, Miscellaneous
More Applications of The Pumping Lemma
The Recursive Descent Algorithm
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
CMPE 152: Compiler Design December 4 Class Meeting
Compiler Design 3. Lexical Analyzer, Flex
Presentation transcript:

Compiler Design Yacc Example "Yet Another Compiler Compiler" Kanat Bolazar

Lex and Yacc Two classical tools for compilers: Lex: A Lexical Analyzer Generator Yacc: “Yet Another Compiler Compiler” (Parser Generator) Lex creates programs that scan your tokens one by one. Yacc takes a grammar (sentence structure) and generates a parser. Lexical Rules Grammar Rules Lex Yacc Input yylex() yyparse() Parsed Input

Lex and Yacc Lex and Yacc generate C code for your analyzer & parser. Lexical Rules Grammar Rules C code Lex C code Yacc Parsed Input Input yylex() yyparse() token stream char stream C code C code Lexical Analyzer (Tokenizer) Parser

Flex, Yacc, Bison, Byacc Often, instead of the standard Lex and Yacc, Flex and Bison are used: Flex: A fast lexical analyzer (GNU) Bison: A drop-in replacement for (backwards compatible with) Yacc Byacc is Berkeley implementation of Yacc (so it is Yacc). Resources: http://en.wikipedia.org/wiki/Flex_lexical_analyser http://en.wikipedia.org/wiki/GNU_Bison The Lex & Yacc Page (manuals, links): http://dinosaur.compilertools.net/

Yacc: A Standard Parser Generator Yacc is not a new tool, and yet, it is still used in many projects. Yacc syntax is similar to Lex/Flex at the top level. Lex/Flex rules were regular expression – action pairs. Yacc rules are grammar rule – action pairs. declarations %% rules programs

Yacc Examples: Calculator A standard Yacc example is the int-valued calculator. Appendix A of Yacc manual at Lex and Yacc Page shows such a calculator. We'll examine this example in parts. Let's start with four operations: E -> E + E | E – E | E * E | E / E Note that this grammar is ambiguous because 2 + 5 * 7 could be parsed 2 + 5 first or 5 * 7 first.

Yacc Calculator Example: Declarations %{ # include <stdio.h> # include <ctype.h> int regs[26]; int base; %} %start list %token DIGIT LETTER %left '+' '-' %left '*' '/' '%' %left UMINUS /* precedence for unary minus */ Directly included C code list is our start symbol; a list of one-line statements / expressions. DIGIT & LETTER are tokens; (other tokens use ASCII codes, as in '+', '=', etc) Precedence and associativity (left) of operators: +, - have lowest precedence *, / have higher precedence Unary minus has highest precedence

Yacc Calculator Example: Rules %% /* begin rules section */ list : /* empty */ | list stat '\n' | list error '\n' { yyerrok; } ; stat : expr { printf( "%d\n", $1 ); } | LETTER '=' expr { regs[$1] = $3; } number: DIGIT { $$ = $1; base = ($1==0) ? 8 : 10; } | number DIGIT { $$ = base * $1 + $2; } list: a list of one-line statements / expressions. Error handling allows a statement to be corrupt, but list continues with next statement. statement: expression to calculate, or assignment number: made up of digits (tokenizer should handle this, but this is a simple example).

Yacc Calculator Example: Rules, cont'd expr : '(' expr ')' { $$ = $2; } | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '*' expr { $$ = $1 * $3; } | expr '/' expr { $$ = $1 / $3; } | '-' expr %prec UMINUS { $$ = - $2; } | LETTER { $$ = regs[$1]; } | number ; Unary minus Letter: Register/var

Yacc Calculator Example: Programs (C Code) %% /* start of programs */ yylex() { /* lexical analysis routine */ /* returns LETTER for a lower case letter, yylval = 0 through 25 */ /* return DIGIT for a digit, yylval = 0 through 9 */ /* all other characters are returned immediately */ int c; while( (c=getchar()) == ' ' ) {/* skip blanks */ } /* c is now nonblank */ if( islower( c ) ) { yylval = c - 'a'; return ( LETTER ); } if( isdigit( c ) ) { yylval = c - '0'; return( DIGIT ); return( c );