Yet Another Compiler Compiler

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

Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
Courtesy Costas Busch - RPI1 Positive Properties of Context-Free languages.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Language processing: introduction to compiler construction Andy D. Pimentel Computer Systems Architecture group
Costas Busch - RPI1 Positive Properties of Context-Free languages.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
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.
1 More Applications of The Pumping Lemma. 2 The Pumping Lemma: there exists an integer such that for any string we can write For infinite context-free.
1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)
Fall 2006Costas Busch - RPI1 Lex. Fall 2006Costas Busch - RPI2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found.
Costas Busch - RPI1 More Applications of the Pumping Lemma.
Parser construction tools: YACC
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
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.
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 컴파일러 입문.
YACC Example Taken from LEX & YACC Simple calculator a = a a=10 b = 7 c = a + b c c = 17 pressure = ( ) * 16.4 $
Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad.
1 Lex. 2 Lex is a lexical analyzer Var = ; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:
Prof Busch - LSU1 YACC Yet Another Compiler Compiler.
Syntax Specification with YACC © Allan C. Milne Abertay University v
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Lex.
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.
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.
Applications of Context-Free Grammars (CFG) Parsers. The YACC Parser-Generator. by: Saleh Al-shomrani.
Introduction to YACC CS 540 George Mason University.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Costas Busch - LSU1 Lex. Costas Busch - LSU2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found the lex program takes.
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 (Yet Another Compiler-Compiler) Chung-Ju Wu
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Compiler Construction Sohail Aslam Lecture Parser Generators  YACC – Yet Another Compiler Compiler appeared in 1975 as a Unix application.  The.
LEX & Yacc Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
CS 310 – Fall 2008 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Yacc.
Language processing: introduction to compiler construction
Syntax Analysis Part III
Textbook:Modern Compiler Design
Java CUP.
Compiler Construction
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
TDDD55- Compilers and Interpreters Lesson 2
Syntax Analysis Part III
Bison: Parser Generator
Simple, efficient;limitated
Syntax Analysis Part III
Syntax Analysis Part III
Syntax Analysis Part III
Compiler Construction
Syntax-Directed Translation
Yet Another Compiler Compiler
Compiler Lecture Note, Miscellaneous
More Applications of The Pumping Lemma
Compiler Structures 7. Yacc Objectives , Semester 2,
Saumya Debray The University of Arizona Tucson, AZ 85721
Compiler Design Yacc Example "Yet Another Compiler Compiler"
CMPE 152: Compiler Design December 4 Class Meeting
Systems Programming & Operating Systems Unit – III
Faculty of Computer Science and Information System
Presentation transcript:

Yet Another Compiler Compiler YACC Yet Another Compiler Compiler 11/28/2018 Costas Busch - RPI

Yacc is a parser generator Input: A Grammar Output: A parser for the grammar Reminder: a parser finds derivations 11/28/2018 Costas Busch - RPI

Example grammar: The yacc code: expr -> ( expr ) | expr '+' expr | INT ; The yacc code: expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | - expr | INT ; 11/28/2018 Costas Busch - RPI

expr => expr + expr => expr * expr + expr => 10*3 + 4 Exampe Input: 10 * 3 + 4 Yacc Derivation: expr => expr + expr => expr * expr + expr => 10*3 + 4 11/28/2018 Costas Busch - RPI

Resolving Ambiguities %left '+', '-' %left '*', '/' %left UMINUS %% expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | '-' expr %prec UMINUS | INT ; 11/28/2018 Costas Busch - RPI

Actions %left '+', '-' %left '*', '/' %left UMINUS %% expr : '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} ; 11/28/2018 Costas Busch - RPI

A Complete Yacc program (EXPR1.y) %union{ int int_val; } %left '+', '-' %left '*', '/' %left UMINUS %token <int_val> INT %type <int_val> expr %start program %% 11/28/2018 Costas Busch - RPI

program : expr {printf("Expr value = %d \n", $1);} | error {printf("YACC: syntax error near line %d \n", linenum); abort();} ; expr : '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} %% #include "lex.yy.c" 11/28/2018 Costas Busch - RPI

Execution Example Input: Output: 10 + 20*(3 - 4 + 25) Expr value = 490 11/28/2018 Costas Busch - RPI

The Lex Code (expr1.l) %{ int linenum=1; int temp_int; %} %% \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */; "+" {return '+';} "-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';} "(" {return '(';} 11/28/2018 Costas Busch - RPI

[0-9]+ {sscanf(yytext, "%d", &temp_int); yylval.int_val = temp_int; return INT;} . {printf("LEX: unknown input string found in line %d \n", linenum); abort();} 11/28/2018 Costas Busch - RPI

cc y.tab.c -ly -ll -o myparser Compiling: yacc YaccFile lex LexFile cc y.tab.c -ly -ll -o myparser Executable: myparser 11/28/2018 Costas Busch - RPI

Another Yacc Program (EXPR2.y) %union{ int int_val; } %left '+', '-' %left '*', '/' %left UMINUS %token <int_val> INT %type <int_val> expr %start program %% 11/28/2018 Costas Busch - RPI

| error {printf("YACC: syntax error near line %d \n", linenum); program : stmt_list | error {printf("YACC: syntax error near line %d \n", linenum); abort();} ; stmt_list : stmt_list stmt | stmt stmt : expr ';' {printf("Expr value = %d \n", $1);} 11/28/2018 Costas Busch - RPI

| '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} ; %% expr : '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} ; %% #include "lex.yy.c" 11/28/2018 Costas Busch - RPI

Execution Example Input: Output: 10 + 20*(30 -67) / 4; 34 * 35 - 123 + -001; 17*8/6; Output: Expr value = -175 Expr value = 1066 Expr value = 22 11/28/2018 Costas Busch - RPI

Lex Code (expr2.l) %{ int linenum=1; int temp_int; %} %% \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */; 11/28/2018 Costas Busch - RPI

[0-9]+ {sscanf(yytext, "%d", &temp_int); yylval.int_val = temp_int; "+" {return '+';} "-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';} "(" {return '(';} ";" {return ';';} [0-9]+ {sscanf(yytext, "%d", &temp_int); yylval.int_val = temp_int; return INT;} . {printf("LEX: unknown input string found in line %d \n", linenum); abort();} 11/28/2018 Costas Busch - RPI

Another Yacc Program (EXPR3.y) %union{ int int_val; char *str_val; } %left '+', '-' %left '*', '/' %left UMINUS %token PRINT %token NEWLINE %token <str_val> STRING %token <int_val> INT %type <int_val> expr %start program %% 11/28/2018 Costas Busch - RPI

| error {printf("YACC: syntax error near line %d \n", linenum); program : stmt_list | error {printf("YACC: syntax error near line %d \n", linenum); abort();} ; stmt_list : stmt_list stmt | stmt stmt : expr ';' {printf("expression found\n");} | PRINT expr ';' {printf("%d", $2);} | PRINT STRING ';' {printf("%s", $2);} | PRINT NEWLINE ';' {printf("\n");} 11/28/2018 Costas Busch - RPI

| '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} ; %% expr : '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '-' expr %prec UMINUS {$$ = -$2;} | INT {$$ = $1;} ; %% #include "lex.yy.c" 11/28/2018 Costas Busch - RPI

Execution Example Input: Output: print "The value of expression 123 * 25 is "; print 123 * 25; print newline; 10 + 5 * 8; print "end of program"; Output: The value of expression 123 * 25 is 3075 expression found end of program 11/28/2018 Costas Busch - RPI

Lex Code (expr3.l) %{ int linenum=1; int temp_int; char temp_str[200]; %} %% \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */; 11/28/2018 Costas Busch - RPI

"newline" {return NEWLINE;} "print" {return PRINT;} "newline" {return NEWLINE;} 11/28/2018 Costas Busch - RPI

[0-9]+ {sscanf(yytext, "%d", &temp_int); yylval.int_val = temp_int; return INT;} \"[^"\n]*\" {strncpy(temp_str, &(yytext[1]), strlen(yytext)-2); temp_str[strlen(yytext)-2] = (char) 0; yylval.str_val = temp_str; return STRING;} . {printf("LEX: unknown input string found in line %d \n", linenum); abort();} 11/28/2018 Costas Busch - RPI