Professional Documents
Culture Documents
Compiler Design-
Laboratory Manual.
For 5th Semester students.
By
PES-INSTITUTE OF TECHNOLOGY
DHEERAJ.D
Asst.Prof
(Autonomous Institute under VTU , Belgaum and UGC , New Delhi)100 feet ring
road, BSK 3rd stage, Banglore-560 085.Phone: 080-2672 1983, Fax: 080-2672 0886.
http://pesit.pes.edu
Compiler Design-Laboratory Manual.
%{
#include<stdio.h>
int F0=0,F1=0,F2=0,error=0,l1=0,l2=0;
%}
verb am|run|sit|did|study|is|large|go|come
subject [a-zA-Z]+
compnd “and”|”but”|”also”|”either”|”neither”|”yet”|”still”|”consequences”
%%
{verb} { if(F2==1)
l2=1;
F2=1;
if(F1==0)
error=1;
{compnd} { F0=1; }
{subject} { if(F1!=0)
l1=1;
F1++;
%%
main()
yylex();
exit(0);
else
Aliter
%{
#include<stdio.h>
int flag=0;
%}
%%
if|
because|
or|
either|
nor|
neither|
but{flag=1;}}
%%
main()
{
printf("Enter the sentence");
yylex();
if(flag)
{
printf("compound");
else
printf("simple");
}
}
2. Write a lex Program to count the number of keywords and identifiers in a sentence.
%{
#include<stdio.h>
static int key_word=0;
static int identifier=0;
%}
%%
"include"|"for"|"define" {key_word++;printf("keyword found");}
"int"|"char"|"float"|"double" {identifier++;printf("identifier found");}
%%
int main()
{
printf("enter the sentence");
yylex();
printf("keyword are: %d\n and identifier are: %d\n",key_word,identifier);
}
%{
#include<stdio.h>
Void octtodeci(int);
%}
%%
[0-7]+{octtodeci(atoi(yytext));}
%%
int main()
Yylex();
int sum=0;
int r=0;
int i=0;
while(num!=0)
r=num%10;
sum=sum+r*po(8,i);
i++;
num=num/10;
int j=0;
int k=1;
for(j=0;j<m;j++)
k=k*a;
Return k;
4. Write a YACC Program to check whether given string a^nb^n is accepted by the
grammar.
%{
#include<stdio.h>
%}
%token A B
%%
S : ASB
| AB
%%
yyerror()
int yylex()
char ch;
ch=getchar();
if (ch==’a’)
return A;
else if (ch==’b’)
return B;
else if(ch==’\n’)
return 0;
int main()
yyparse();
Return 0;
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token ID
%left '+','-'
%left '*','/'
%%
E:E'+'E { printf("valid expression);}
| E'-'E { printf("valid expression);}
| E'/'E { printf("valid expression);}
| E'*'E { printf("valid expression);}
| ID
;
%%
int yylex()
{
Char ch;
Ch=getchar();
if(isalnum(ch))
return ID;
else if(ch==’\n’)
return 0;
yyerror()
int main()
{
6. Write a YACC Program to identify an input for the grammar a^nb (n>=10)
%{
#include<stdio.h>
#include “y.tab.h”
%}
%%
[aA] return A;
[bB] return B;
%%
—————————————–
6.y
%{
#include<stdio.h>
%}
%token A B
%%
S: X B
X: X A | AAAAAAAAAA
%%
main()
if(yyparse())
printf(“\nValid expression”);
exit(0);
int yyerror()
printf(“\nInvalid expression\n”);
return 1;
int yywrap()
return 1;
ANTLR takes as input a grammar that specifies a language and generates as output source code
for a recognizer for that language. At the moment, ANTLR supports generating code in the
programming languages Ada95, Action Script, C, C#, Java, JavaScript, Objective-C, Perl,
Python, and Ruby. A language is specified using a context-free grammar which is expressed
using Extended Backus Naur Form EBNF.
ANTLR allows generating parsers, lexers, tree parsers, and combined lexer-parsers. Parsers can
automatically generate abstract syntax trees which can be further processed with tree parsers.
ANTLR provides a single consistent notation for specifying lexers, parsers, and tree parsers. This
is in contrast with other parser/lexer generators and adds greatly to the tool's ease of use.
By default, ANTLR reads a grammar and generates a recognizer for the language defined by the
grammar (i.e. a program that reads an input stream and generates an error if the input stream
does not conform to the syntax specified by the grammar). If there are no syntax errors, then the
default action is to simply exit without printing any message. In order to do something useful
with the language, actions can be attached to grammar elements in the grammar. These actions
are written in the programming language in which the recognizer is being generated. When the
recognizer is being generated, the actions are embedded in the source code of the recognizer at
the appropriate points. Actions can be used to build and check symbol tables and to emit
instructions in a target language, in the case of a compiler.
As well as lexers and parsers, ANTLR can be used to generate tree parsers. These are
recognizers that process abstract syntax trees which can be automatically generated by parsers.
These tree parsers are unique to ANTLR and greatly simplify the processing of abstract syntax
trees.
• SelectconfigureANTLR project.
• Note: if there are no errors in your program, ANTLR generates a lexer and a parser file
for you.
• Go to the interpreter, give the input for the grammar, ANTLR generates a parse tree as
output.
1. Write an ANTLr grammar to accept the pascal statement READ(Value) and print a
parse tree for the same.
grammar Read;
options {
language = Java;
}
tokens{
READ = 'READ';
OB = '(';
CB = ')';
CM = ',';
@members
{
/*public static void main (String [] args) throws Exception
{
ReadLexer lex = new ReadLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);
ReadParser parser = new ReadParser(tokens);
try
{
parser.expr();
}
catch(RecognitionException e)
{
e.printStackTrace();
}
}*/
}
IDLIST : ID(((CM)ID)+)?;
fragment ID : AL+;
fragment AL : 'a'..'z'|'A'..'Z';
grammar Test;
options {
language = Java;
}
tokens
{
PLUS = '+';
MINUS = '-';
MULT = '*';
DIV = '/';
ASSIGNMENT =':=';
}
@members
{
public static void main(String[] args)
throws Exception
{
TestLexer lex = new TestCalLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lex);
TestParser parser = new TestParser(tokens);
try
{
parser.expr();
}
catch(RecognitionException e)
{
e.printStackTrace();
}
}
}
/*Lexer Rules*/
ID: A+;
NUMBER: (DIGIT) + ('.' (DIGIT)+)? ;
WS: ('\t'|' '|'\r'|'\n')+ ;
fragment A : 'a'..'z'|'A'..'Z';
/*fragment:Special construct in ANTLR to describe a part of another rule*/
fragment DIGIT: '0'..'9' ;
/*Parser Rules*/
stmt: ID ASSIGNMENT expr;
expr:term((PLUS|MINUS)term)*;
term:factor((MULT|DIV)factor)*;
factor: ID | NUMBER;
grammar Prog1;
options {
language = Java;
}
tokens
{
ASSIGN = ':=';
PLUS = '+';
MINUS = '-';
MULT = '*';
DIV = '/';
TO = 'TO';
DO = 'DO';
BEGIN = 'BEGIN';
END = 'END';
WRITE = 'WRITE';
SEMICLN = ';';
FOR = 'FOR';
OB = '(';
CB = ')';
CM = ',';
}
ID: A+;
NUMBER : (DIGIT)+('.' (DIGIT)+)? ;
//WS: ('\t'|' '|'\r'|'\n')+ ;
fragment A : 'a'..'z'|'A'..'Z';
fragment DIGIT: '0'..'9';
for: FOR ID ASSIGN NUMBER TO NUMBER DO BEGIN stmtlist END;
stmtlist: stmt+;
stmt: write | ID ASSIGN expr SEMICLN;
expr:term((PLUS|MINUS)term)* ;
term:factor((MULT|DIV)factor)*;
factor: ID | NUMBER;
write : WRITE OB idlist CB SEMICLN;
idlist : ID((CM ID)+)?;
grammar Prog2;
options
{
language = Java;
}
sentence : (VERBS|NOUN|ADJECTIVE)+;
09 CS 303
3 Program #2 Write a lex Program to count the number of keywords and identifiers in a sentence.
Write a YACC Program to check whether given string a^nb^n is accepted by the
5 Program #4
grammar.
7 Program #6 Write a YACC Program to identify an input for the grammar a^nb (n>=10).
Instruction
8 Instruction class on ANTLR (Another tool for Language Recognition).
class
Write an ANTLr grammar to accept the pascal statement READ(Value) and print a
9 Program #1
parse tree for the same.
11 Program #3 Write an ANTLr grammar to accept a block of PASCAL statements between begin and
end and print the parse tree for the same.
12 Program #4 Write an ANTLr grammar to decide whether given sentence is simple or compound.