Using CookCC.  Use *.l and *.y files.  Proprietary file format  Poor IDE support  Do not work well for some languages.

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

Compiler construction in4020 – lecture 2 Koen Langendoen Delft University of Technology The Netherlands.
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
CPSC Compiler Tutorial 9 Review of Compiler.
SableCC SableCC is developed by professors and graduate students at McGill University and is open source (licensed under the Apache License, Version 2.0)‏
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.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Compiler Summary Mooly Sagiv html://
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
Parser construction tools: YACC
Game Scripting By: Nicholas Haines. Aurora Neverwinter Toolset.
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
LEX and YACC work as a team
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Game Scripting by: Nicholas Haines. What is Scripting? Interpreted Language Interpreted Language –As the game runs.
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.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
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
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Introduction to Lex Fan Wu
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.
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.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
CS 614: Theory and Construction of Compilers Lecture 7 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Writing Parsers with Ruby
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
Parser Generation Using SLK and Flex++ Copyright © 2015 Curt Hill.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
By Mr. Muhammad Pervez Akhtar
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
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.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Lecture Transforming Data: Using Apache Xalan to apply XSLT transformations Marc Dumontier Blueprint Initiative Samuel Lunenfeld Research Institute.
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.
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design (40-414) Main Text Book:
Tutorial On Lex & Yacc.
Using SLK and Flex++ Followed by a Demo
Compiler Construction
Context-free Languages
Compiler Lecture 1 CS510.
Programmazione I a.a. 2017/2018.
User-Defined Functions
Starting JavaProgramming
C Preprocessor(CPP).
Subject Name:Sysytem Software Subject Code: 10SCS52
CS 3304 Comparative Languages
Yacc Yacc.
CMPE 152: Compiler Design December 4 Class Meeting
Systems Programming & Operating Systems Unit – III
Compiler Design 3. Lexical Analyzer, Flex
Presentation transcript:

Using CookCC

 Use *.l and *.y files.  Proprietary file format  Poor IDE support  Do not work well for some languages

 Difficult to read ▪ No syntax highlighting ▪ No variable / function usage highlight and analysis etc.  Difficult to write ▪ Need to hook up lexer and parser manually ▪ No context sensitive hints (function call and parameter hints) ▪ No instant code error checking within the IDE ▪ No auto-completions (auto-casting, auto-import, etc) ▪ Fragmented code  Difficult to maintain ▪ Multiple files to manage (*.l and *.y) ▪ No code reformatting ▪ No refactoring  Difficult to document

BEGIN(FIRSTCCL); return '['; } "{-}" return CCL_OP_DIFF; "{+}" return CCL_OP_UNION; /* Check for :space: at the end of the rule so we don't * wrap the expanded regex in '(' ')' -- breaking trailing * context. */ "{"{NAME}"}"[[:space:]]? { register Char *nmdefptr; int end_is_ws, end_ch; end_ch = yytext[yyleng-1]; end_is_ws = end_ch != '}' ? 1 : 0; if(yyleng-1 < MAXLINE) { strcpy( nmstr, yytext + 1 ); }

for ( i = $2; i <= $4; ++i ) ccladd( $1, i ); cclsorted = cclsorted && ($2 > lastchar); lastchar = $4; } $$ = $1; } | ccl CHAR { ccladd( $1, $2 ); cclsorted = cclsorted && ($2 > lastchar); lastchar = $2; /* Do it again for upper/lowercase */ if (sf_case_ins() && has_case($2)){ $2 = reverse_case ($2); ccladd ($1, $2); cclsorted = cclsorted && ($2 > lastchar); lastchar = $2; }

 Supports XML input  Solves the problem related to file format. ▪ Better support for different languages ▪ Patterns / Rules stand out more  Still do not solve other problems.  Integrated lexer and parser generator  Unfortunately, most still require manual hook up between the generated lexer and parser.

 Advantages:  Write code as normal and take the full advantage of the modern IDEs ▪ Syntax highlighting ▪ Context sensitive hints ▪ Code usage and analysis ▪ Refactoring ▪ Auto-completion ▪ Instant error checking ▪ etc  Disadvantages:  Need to write a language specific annotation parser ▪ Java rules and nothing else matters

 Integrated lexer and parser (LALR (1))  Can specify lexer and parser separately.  If the lexer / parser are specified together, they are automatically hooked up.  Supports multiple input file types  XML (DTD): *.xccDTD  Yacc: *.y  Java annotation: *.java (launched within Java APT)  Support multiple output formats  Uses the powerful FreeMarker template engine for code generation.FreeMarker  Java ▪ Case by case optimized lexer and parser ▪ No runtime library required. Very small footprint. ▪ Multiple lexer / parser DFA tables options (ecs, compressed).  Plain text (for table extraction)  XML (for input file conversion)  Yacc (for input file conversion)  More output languages can be added

Quick Tutorial

 Put cookcc.jar in the project library path.  This jar is only required for compile time.  All CookCC annotations are compile time annotations (not required for runtime). mark a class that needs the lexer and parser.  Generated class would be the parent class of the current class. In this example, it would be Parser.  lexerTable and parserTable options are optional

 Parser is an empty class  CookCCByte contains functions that would appear in the generated class. ▪ Generated class no longer extends CookCCByte ▪ CookCCByte is not required for runtime  Extend CookCCByte for byte parsing and CookCCChar for unicode parsing.  Class scope is copied to the generated class.  File header (i.e. the copyright message) and class header will be copied to the generated class.

specifies name of the regular expressions patterns that would be used regularly later on.  Can be annotated on any functions in any order.  Avoid creating cyclic name references. is a collection of shortcuts. specific the regular expression pattern, and which states this pattern is used in. The function this annotation marked is called when the pattern is matched.  Usually the function scope should be either protected or package since they are called by the generated class only.  Backslash (\) needs to be escaped inside Java string, resulting in double backslash (\\)

 Function returns void  The lexer would call this function and then move on to match the next potential pattern.

 Function returns a non-int value needs to contain the terminal token that would be returned. The return value from the function is going to be the value associated with this terminal.

 Function returns an int value  The lexer would return the value immediately.

@Lexs is a collection patterns

marks a Enum to specify tokens shared between the lexer/parser specify the token type. Tokens marked later on have higher precedences.  Tokens not marked would inherit the type and precedence of the previous token.  Symbols such as + - * / etc would have to have a name since they can’t be fit in the enum declaration. This restriction is imposed by CookCC per se, not annotation based parsing in general.

specifies a single grammar rule.  args specifies the arguments to be passed to the function  Advantages  No more cryptic names like $1 $2 or having to specify the types of the variable elsewhere.

 Function returns void  The value associated with the non-terminal of the LHS is null.

 Function returns a non-int value  The return value is automatically associated with the non-terminal on the LHS.

 Function returns an int value  This function is used by the grammar start non- terminal to signal the exit of the parser with the particular value.  It can be used by error processing functions as well.

is a collection productions.

 setInput (…) to set up the input source  setBufferSize to improve lexer speed.  yyLex () for lexer only scanner  yyParse () for parser

 Run java annotation processing tool (APT) using CookCC as the processor to generate the class needed.  apt –nocompile –s. –classpath cookcc.jar;. Calculator.java  Unless the lex/parse patterns/rules are changed, it is not necessary to re-generate new classes.  Can be refactored at anytime.

 Using Ant task is easier.  Setup task  Execute task

 Options

 Dr. Stott Parker  For helpful discussions  Related Work  SPARK for Python SPARK ▪ Uses Python doc string to specify the lexer / parser. ▪ Aycock, J. "Compiling Little Languages in Python", Proceedings of the Seventh International Python Conference, p100, 1998.

 Project home:   License  New BSD License New BSD License