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
Lexical and Syntax Analyzer
(PA1)
Md. Zahurul Islam
CRBLP, BRAC University
Project Details
 You will construct a lexer and a parser
for C- language, a subset of the C
language.
 Portions
 Lexical Analyzer (lexer)
 Syntax Analyzer (parser)
 Tools
 JLex
 JCUP
Md. Zahurul Islam
CRBLP, BRAC University
JLex
 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.
Md. Zahurul Islam
CRBLP, BRAC University
JLex Specification
user code
%%
JLex directives
%%
regular expression rules
Md. Zahurul Islam
CRBLP, BRAC University
JLex Specification
 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.
Md. Zahurul Islam
CRBLP, BRAC University
JLex Specification
 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




Md. Zahurul Islam
%implements
%class
%function
%type
CRBLP, BRAC University
JLex Specification
 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.
Md. Zahurul Islam
CRBLP, BRAC University
CUP (Constructor of Useful Parsers)
 CUP is written in Java, uses
specifications including embedded
Java code, and produces parsers
which are implemented in Java.
Md. Zahurul Islam
CRBLP, BRAC University
CUP Specification Syntax
Package and import specification
User code components
Symbol lists
Precedence and associativity
declaration
 The grammar




Md. Zahurul Islam
CRBLP, BRAC University
CUP Specification Syntax
 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.*;
Md. Zahurul Islam
CRBLP, BRAC University
CUP Specification Syntax
 User Code Components
 Optional declarations that allow user
code to be included as part of the
generated parser.
action code {: ... :};
parser code {: ... :};
Md. Zahurul Islam
CRBLP, BRAC University
CUP Specification Syntax
 Symbol List
 These declarations are responsible for
naming and supplying a type for each
terminal and non-terminal symbol that
appears in the grammar.
terminal classname name1, name2, ...;
non terminal classname name1, name2, ...;
Md. Zahurul Islam
CRBLP, BRAC University
CUP specification Syntax
 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…];
Md. Zahurul Islam
CRBLP, BRAC University
CUP Specification Syntax
 The Grammar
 The final section of a CUP declaration
provides the Grammar.
Md. Zahurul Islam
CRBLP, BRAC University
Summing Up
 Project Details
 JLex
 JCUP
Md. Zahurul Islam
CRBLP, BRAC University
Related documents