Syntax Analysis The Parser Generator (BYacc/J)

Similar documents
Yacc: A Syntactic Analysers Generator

LECTURE 11. Semantic Analysis and Yacc

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Using an LALR(1) Parser Generator

Syntax Analysis Part IV

HW8 Use Lex/Yacc to Turn this: Into this: Lex and Yacc. Lex / Yacc History. A Quick Tour. if myvar == 6.02e23**2 then f(..!

Lex and Yacc. A Quick Tour

Compiler Lab. Introduction to tools Lex and Yacc

COMPILER CONSTRUCTION Seminar 02 TDDB44

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Lex and Yacc. More Details

Parsing How parser works?

TDDD55 - Compilers and Interpreters Lesson 3

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

Introduction to Compiler Design

TDDD55- Compilers and Interpreters Lesson 3

Compiler Construction

Last Time. What do we want? When do we want it? An AST. Now!

Compiler Construction

Syntax-Directed Translation

Lexical and Syntax Analysis

CSE302: Compiler Design

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Chapter 2: Syntax Directed Translation and YACC

Automatic Scanning and Parsing using LEX and YACC

PRACTICAL CLASS: Flex & Bison

Yacc Yet Another Compiler Compiler

CSCI Compiler Design

An Introduction to LEX and YACC. SYSC Programming Languages

Lexical Analysis - Flex

I. OVERVIEW 1 II. INTRODUCTION 3 III. OPERATING PROCEDURE 5 IV. PCLEX 10 V. PCYACC 21. Table of Contents

Compiler construction in4020 lecture 5

Introduction to Lex & Yacc. (flex & bison)

Ray Pereda Unicon Technical Report UTR-03. February 25, Abstract

Parsing and Pattern Recognition

Lexical and Parser Tools

Parser Generators. Mark Boady. August 14, 2013

As we have seen, token attribute values are supplied via yylval, as in. More on Yacc s value stack

Yacc. Generator of LALR(1) parsers. YACC = Yet Another Compiler Compiler symptom of two facts: Compiler. Compiler. Parser

Lex & Yacc (GNU distribution - flex & bison) Jeonghwan Park

Compiler construction 2002 week 5

Big Picture: Compilation Process. CSCI: 4500/6500 Programming Languages. Big Picture: Compilation Process. Big Picture: Compilation Process.

Lecture 14 Sections Mon, Mar 2, 2009

Lex & Yacc. by H. Altay Güvenir. A compiler or an interpreter performs its task in 3 stages:

A Bison Manual. You build a text file of the production (format in the next section); traditionally this file ends in.y, although bison doesn t care.

LECTURE 3. Compiler Phases

Principles of Programming Languages

Introduction to Yacc. General Description Input file Output files Parsing conflicts Pseudovariables Examples. Principles of Compilers - 16/03/2006

Preparing for the ACW Languages & Compilers

Lex & Yacc. By H. Altay Güvenir. A compiler or an interpreter performs its task in 3 stages:

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

Compiler construction 2005 lecture 5

Fall Compiler Principles Lecture 4: Parsing part 3. Roman Manevich Ben-Gurion University of the Negev

Applications of Context-Free Grammars (CFG)

Big Picture: Compilation Process. CSCI: 4500/6500 Programming Languages. Big Picture: Compilation Process. Big Picture: Compilation Process

Compiler Design 1. Yacc/Bison. Goutam Biswas. Lect 8

COP4020 Programming Assignment 1 CALC Interpreter/Translator Due March 4, 2015

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

Automated Tools. The Compilation Task. Automated? Automated? Easier ways to create parsers. The final stages of compilation are language dependant

CS 415 Midterm Exam Spring 2002

JFlex. Lecture 16 Section 3.5, JFlex Manual. Robb T. Koether. Hampden-Sydney College. Mon, Feb 23, 2015

Project 3 - Parsing. Misc Details. Parser.java Recursive Descent Parser... Using Lexer.java

IN4305 Engineering project Compiler construction

Compiler Construction

COMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis

Over-simplified history of programming languages

I/O and Parsing Tutorial

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

Using JFlex. "Linux Gazette...making Linux just a little more fun!" by Christopher Lopes, student at Eastern Washington University April 26, 1999

COP 3402 Systems Software Syntax Analysis (Parser)

Abstract Syntax Trees Synthetic and Inherited Attributes

Simple Lexical Analyzer

COSE312: Compilers. Lecture 1 Overview of Compilers

Compiler Construction Assignment 3 Spring 2018

Topic 5: Syntax Analysis III

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Lecture 12: Parser-Generating Tools

CS4850 SummerII Lex Primer. Usage Paradigm of Lex. Lex is a tool for creating lexical analyzers. Lexical analyzers tokenize input streams.

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Compilers. Compiler Construction Tutorial The Front-end

Compiler Front-End. Compiler Back-End. Specific Examples

COMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )

Context-free grammars

Compil M1 : Front-End

JFlex Regular Expressions

A program that performs lexical analysis may be termed a lexer, tokenizer, or scanner, though scanner is also a term for the first stage of a lexer.

A simple syntax-directed

ASTs, Objective CAML, and Ocamlyacc

Extending xcom. Chapter Overview of xcom

Lab 2. Lexing and Parsing with Flex and Bison - 2 labs

JavaCUP. There are also many parser generators written in Java

Compiler Construction: Parsing

Abstract Syntax. Mooly Sagiv. html://

Semantic Analysis Attribute Grammars

TDDD55- Compilers and Interpreters Lesson 2

Component Compilers. Abstract

Syntax Analysis Top Down Parsing

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

The Structure of a Syntax-Directed Compiler

CS143 Handout 12 Summer 2011 July 1 st, 2011 Introduction to bison

Transcription:

Syntax Analysis The Parser Generator (BYacc/J) CMPSC 470 Lecture 09-2 Topics: Yacc, BYacc/J A. Yacc Yacc is a computer program that generate LALR parser. Yacc stands for Yet Another Compiler-Compiler. It is originally developed and written in B programming language by Stephen C. Johnson at AT&T Corporation in the early 1970s, and rewritten in C. Yacc program 1. Takes, as input, a program containing CFG and the action of each production, 2. Create LALR parse table, and 3. Generate C program. Yacc usually works with a lexical analyzer generator, such as Lex or Flex. How to use Yacc program 1. From source translate.y file, generate y.tab.c file using yacc program as follows: yacc translate.y 2. Compile the C program, and make a.out file: cc y.tab.c 3. Run a.out file Format of yacc file. declaration translation rule supporting C routine

B. BYacc/J BYacc/J is an extension of the Berkeley YACC. BYacc/J generates C/C++ and Java parser. (using J flag) Its Windows and Linux binaries are available at http://byaccj.sourceforge.net/ Steps to use BYacc/J 1. Download binaries for Win32 or Linux from http://byaccj.sourceforge.net/#download 2. Unzip the yacc.exe file 3. Compile your myparser.y as follows: yacc.exe J Parser.y 4. It generates Parser.java and ParserVal.java parser files. C. BYacc/J input format and output format % Import java.io.* % %token ADD MUL %token <ival> NUM %type <dval> expr %type <obj> exprs Parser.y %left ADD %left MUL start : exprs action 1 exprs : exprs expr SEMI action 2 action 3 expr : expr ADD expr action 4 expr MUL expr action 5 LPAREN expr RPAREN action 6 NUM action 7 /* ParserVal yylval is already defined */ private int yylex () yylval = new ParserVal(0) int yyl_return = lexer.yylex() return yyl_return public void yyerror (String error) System.err.println ("Error: " + error) public Parser(Reader r) lexer = new Lexer(r, this) Parser.java import java.io.* public class Parser int yyparse() switch(yyn) case 1 : action 1 break case 10: action 2 break case 12: action 3 break Case 20: action 4 break Case..: action 7 break Case..: action 6 break Case..: action 5 break private int yylex () yylval = new ParserVal(0) int yyl_return = lexer.yylex() return yyl_return public void yyerror (String error) System.err.println ("Error: " + error) public Parser(Reader r) lexer = new Lexer(r, this)

D. Example % import java.io.* import java.util.arraylist % %token <ival> NUM %token ADD SUB MUL DIV SEMI LPAREN RPAREN %type <obj> exprs start %type <ival> expr %left ADD SUB %left MUL DIV start : exprs ArrayList<Integer> vals = (ArrayList<Integer>)$1 for(int i=0 i<vals.size() i++) System.out.println(vals.get(i)) exprs : exprs expr SEMI ArrayList<Integer> vals = (ArrayList<Integer>)$1 int val = $2 vals.add(val) $$ = vals $$ = new ArrayList<Integer>() expr : expr ADD expr $$ = $1 + $3 expr SUB expr $$ = $1 - $3 expr MUL expr $$ = $1 * $3 expr DIV expr int val1 = $1 int val3 = $3 $$ = val1 / val3 LPAREN expr RPAREN int val = $2 $$ = val NUM int num = $1 $$ = num private Lexer lexer private int yylex () int yyl_return = -1 try yylval = new ParserVal(0) yyl_return = lexer.yylex() catch (IOException e) System.out.println("IO error :"+e) return yyl_return public void yyerror (String error) System.err.println ("Error: " + error) public Parser(Reader r) lexer = new Lexer(r, this)

import java.io.* import java.util.arraylist public class Parser public final static short NUM=257 public final static short ADD=258 public final static short RPAREN=264 private Lexer lexer private int yylex () int yyl_return = -1 try yylval = new ParserVal(0) yyl_return = lexer.yylex() catch (IOException e) System.out.println("IO error :"+e) return yyl_return public void yyerror (String error) System.err.println ("Error: " + error) public Parser(Reader r) lexer = new Lexer(r, this) ParserVal yyval //used to return semantic vals from action routines ParserVal yylval//the 'lval' (result) I got from yylex() //############################################################### // method: yyparse : parse input and execute indicated items //############################################################### int yyparse() switch(yyn) //########## USER-SUPPLIED ACTIONS ########## case 1: ArrayList<Integer> vals = (ArrayList<Integer>)val_peek(0).obj for(int i=0 i<vals.size() i++) System.out.println(vals.get(i)) break case 2: ArrayList<Integer> vals = (ArrayList<Integer>)val_peek(2).obj int val = val_peek(1).ival vals.add(val) yyval.obj = vals break case 3: yyval.obj = new ArrayList<Integer>() break case 4: yyval.ival = val_peek(2).ival + val_peek(0).ival break case 5: yyval.ival = val_peek(2).ival - val_peek(0).ival break case 6: yyval.ival = val_peek(2).ival * val_peek(0).ival break case 7: int val1 = val_peek(2).ival int val3 = val_peek(0).ival yyval.ival = val1 / val3 break case 8: int val = val_peek(1).ival yyval.ival = val break case 9: int num = val_peek(0).ival yyval.ival = num break //########## END OF USER-SUPPLIED ACTIONS ##########

E. Declaration %token <ival> NUM1 NUM2 NUM3 %token <dval> REAL1 REAL2 REAL3 %token <sval> ID1 ID2 ID3 %token ADD SUB MUL DIV SEMI LPAREN RPAREN %type <obj> exprs start %type <ival> expr %right ASSIGN %left ADD SUB %left MUL DIV

F. Translation Rules The translation rule should be written as follows: head body 1 semantic action 1 body 2 semantic action 2 body n semantic action n Example) Given grammar GG: llllllll llllllll eeeeeeee εε eeeeeeee eeeeeeee + tttttttt tttttttt tttttttt tttttttt ffffffffffff ffffffffffff ffffffffffff NNNNNN (eeeeeeee) The translation rule can be written as follows: list : list expr SEMI $1.add($2) $$ = $1 $$ = new list() expr : expr ADD term $$ = $1 + $3 term term : term MUL factor $$ = $1 * $3 factor $$ = $1 factor : NUM $$ = str2int($1) LPAREN expr RPAREN $$ = $2

G. Supporting runtime routine part The third part of a yacc file specify member functions or member variables of Parser class. To generate java parser using BYacc/J, the usre must provide two methods in the yacc source: void yyerror(string msg) int yylex(). int yylex() This method must return token ID, or <0 if there is an error, or 0 when it encounters the end of input. Following shows the sample yylex() function that use jflex. private int yylex () int yyl_return = -1 try yylval = new ParserVal(0) yyl_return = lexer.yylex() catch (IOException e) System.err.println("IO error :"+e) return yyl_return The function yylex() returns the token type as integer, and stores its corresponding token attribute to yylval. The token attribute yylval, whose type is ParserVal, can have a string, integer, double, or object value, as defined as follows: public class ParserVal public int ival public double dval public String sval public Object obj void yyerror(string msg) BYacc/J uses this method to provide error messages. Following shows the sample yyerror() function: public void yyerror (String error) System.err.println ("Error: " + error) You can add custom Parser constructor here.

H. Compile options of BYacc/J BYacc/J support several options for java parser. Followings show the options that is useful in this class. -J Generates java parser, instead of generating C/C++ parser. -Jclass=<classname> Changes the name of the Java class. -Jpackage=<packagename> Set the package in which the parser resides. -Jextends=<extendname> Set the superclass -Jnorun Informs BYacc/J to not generate a run() method. You do not need to create a run() method in your assignment. -Jthrows=<excaption_list> Informs BYacc/J to declare thrown exceptions for yyparse() method. The following compile option will be useful in this class: Yacc.exe -Jthrows="Exception" -Jextends=ParserBase -Jnorun -J Parser.y