Download Lexical and Syntax Analyzer (PA1)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Md. Zahurul Islam
CRBLP, BRAC University


You will construct a lexer and a parser for Clanguage, a subset of the C language.
Portions
◦ Lexical Analyzer (lexer)
◦ Syntax Analyzer (parser)

Tools
◦ JLex
◦ JCUP
CRBLP, BRAC University
Md. Zahurul Islam


A lexical analyzer breaks an input stream of
characters into tokens. Writing lexical analyzers
by hand can be a tedious process, so software
tools have been developed to ease this task.
The JLex utility is based upon the Lex lexical
analyzer generator model. Jlex creates a Java
source file for the corresponding lexical
analyzer.
CRBLP, BRAC University
Md. Zahurul Islam
user code
%%
JLex directives
%%
regular expression rules
CRBLP, BRAC University
Md. Zahurul Islam

User Code
◦ The user code section - the first section of the
specification file - is copied directly into the
resulting output file. This area of the specification
provides space for the implementation of utility
classes or return types.
CRBLP, BRAC University
Md. Zahurul Islam

JLex Directives
◦ The JLex directives section is the second part of the
input file. Here, macros definitions are given and
state names are declared.
 Example




%implements
%class
%function
%type
CRBLP, BRAC University
Md. Zahurul Islam

Regular expression rule
◦ The third section contains the rules of lexical
analysis, each of which consists of three parts: an
optional state list, a regular expression, and an
action.
CRBLP, BRAC University
Md. Zahurul Islam

CUP is written in Java, uses specifications
including embedded Java code, and produces
parsers which are implemented in Java.
CRBLP, BRAC University
Md. Zahurul Islam





Package and import specification
User code components
Symbol lists
Precedence and associativity declaration
The grammar
CRBLP, BRAC University
Md. Zahurul Islam

Package and Import Specification
◦ A specification begins with optional package and
import declarations. These have the same syntax,
and play the same role, as the package and import
declarations found in a normal Java program. A
package declaration is of the form:
package name;
import package_name.*;
CRBLP, BRAC University
Md. Zahurul Islam

User Code Components
◦ Optional declarations that allow user code to be
included as part of the generated parser.
action code {: ... :};
parser code {: ... :};
CRBLP, BRAC University
Md. Zahurul Islam

Symbol List
◦ These declarations are responsible for naming and
supplying a type for each terminal and nonterminal symbol that appears in the grammar.
terminal classname name1, name2, ...; non terminal
classname name1, name2, ...;
CRBLP, BRAC University
Md. Zahurul Islam

Precedence and Associativity Declaration
◦ The third section, which is optional, specifies the
precedence and associativity of terminals. This is
useful for parsing with ambiguous grammar.
precedence left terminal [, terminal1…];
precedence right terminal [,terminal1…];
precedence nonassoc terminal [,terminal…];
CRBLP, BRAC University
Md. Zahurul Islam

The Grammar
◦ The final section of a CUP declaration provides the
Grammar.
CRBLP, BRAC University
Md. Zahurul Islam



Project Details
JLex
JCUP
CRBLP, BRAC University
Md. Zahurul Islam
Related documents