Chapter 6 Part IV
Chapter 6 Part IV
Chapter 6 Part IV
Input String – id + id * id
Problem Solving
1. Grammar is 2. Grammar is
S-> aS S-> aS
S-> aT S-> aT
T-> bW T-> bW
W-> c W-> c
Enter Input String: aaabc Enter Input String: abcd
Operator Grammar
• No Ɛ-transition.
• No two adjacent non-terminals.
Eg.
E E op E | id
op + | *
The above grammar is not an operator
grammar but:
E E + E | E* E | id
Operator Precedence
id + * $
id .> .> .>
+ <. .> <. .>
* <. .> .> .>
$ <. <. <. .>
• Syntax:
%{
// Definitions
%}
• 2. Rules Section: The rules section contains a series of rules in the
form: pattern action and pattern must be unintended and action
begin on the same line in {} brackets. The rule section is enclosed
in “%% %%”.
%%
pattern action
%%
• 3. User Code Section: This section contain C statements and additional
functions. We can also compile these functions separately and load with
the lexical analyzer.
Basic Program Structure:
%{
// Definitions
%}
%%
Rules
%%
User code section
• Design of Lexical Analyzer
• Lexical analyzer can either be generated by NFA or by DFA.
• DFA is preferable in the implementation of lex.
• How to run the program:
To run the program, it should be first saved with the extension .l or .lex.
Run the below commands on terminal in order to run the program file.
• It is called to invoke the lexer (or scanner) and each time yylex() is
called, the scanner continues processing the input from where it last
left off.
YACC
• YACC stands for Yet Another Compiler Compiler.
• YACC provides a tool to produce a parser for a given grammar.
• YACC is a program designed to compile a LALR (1) grammar.
• It is used to produce the source code of the syntactic analyzer of the
language produced by LALR (1) grammar.
• The input of YACC is the rule or grammar and the output is a C
program.
YACC
• Full specification looks like:
declarations
%%
rules
%%
programs
• The rules section is made up of one or more grammar rules. A grammar rule has the form:
A : BODY ;
Body section can be written as:
A:BCD;
A:EF;
A:G;
A:BCD
|EF
|G;