







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture was delivered by Ruiz Pereira at Chandra Shekhar Azad University of Agriculture
Typology: Slides
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Tools.ppt
CS4173 Compiler Construction – 2 –
The process of writing a lexical analyzer and parser can be automated by using tools
Compiler construction tools are also known as compiler compilers or compiler generators
Usually scanner generators accept token definitions as regular expressions, while parser generators accept grammar in BNF (or some variation such as EBNF)
CS4173 Compiler Construction – 4 –
Windows versions of Lex and Yacc can be downloaded from:
http://www.bumblebeesoftware.com/downloads.htm
JFlex can be downloaded from:
http://www.jflex.de/download.html
COCO/R can be downloaded from:
http://cs.ru.ac.za/homes/cspt/cocor.htm
CS4173 Compiler Construction – 5 –
JFlex takes as input a script that uses regular expressions to describe a set of lexeme patterns
It generates a Java class implementing a scanner that recognizes those patterns
An action is associated with each pattern, which is a chunk of Java code – each time the scanner recognizes a pattern, the associated action is executed
CS4173 Compiler Construction – 7 –
User Code section:
The user code section contains code that appears outside of the scanner class
Typically, it contains import or package declarations, but can include an additional class if you like.
The code in this section will appear above the generated scanner class in the .java file generated by JFlex
CS4173 Compiler Construction – 8 –
Options and Declarations section contains three things: Specification of various options, such as the name of the scanner class, the return type and name of its "next token" method, etc. Additional code that you want to place into the scanner class. For example, if you need to add fields or helper methods into the scanner class, this is where you put them. Macro definitions that you want to use in your regular expressions.
CS4173 Compiler Construction – 10 –
Lexical Rules section:
This section contains token definitions and actions that should be executed whenever a matching lexeme is found
Most of the time, the action would contain a return statement which will return a value from the scanner method.
In case of patterns that define text that should be ignored, such as whitespace and comments, the action may be empty
CS4173 Compiler Construction – 11 –
Example of Lexical Rules section: [0-9]+ { return new Token(Token.INTCONST, yytext()); }
[a-zA-Z][a-zA-Z0-9] { return new Token(Token.IDENTIFIER, yytext()); }*
"for" { return new Token(Token.KWD_FOR, yytext()); }
CS4173 Compiler Construction – 13 –
Rules to construct regular expressions in JFlex (contd…): The Kleene closure (or "star") operation is specified by the * character, e.g. the expression [a-z][a-z]* specifies strings of lowercase letters with at least one letter in them. As a shorthand for "one or more", the + operator can be used. The expression [a-z]+ is equivalent to [a-z][a-z]. As a shorthand for "zero or one", the? operator can be used. The expression [a-z][a-z]? is equivalent to [a-z] | [a-z][a-z]. Macro names are written in curly braces. So, the expression {Whitespace}+ would match one or more characters of whitespace, assuming an appropriately defined macro called Whitespace. The. character (appearing outside of a string) matches any character in the character set except a newline. So, the expression a. matches all strings that begin with a lowercase "a".