Compiler Design- Laboratory Manual.

Λογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 4 χρόνια και 5 μήνες)

88 εμφανίσεις

PES
-
INSTITUTE OF TECHNOL
OGY

DHEERAJ.D

Asst.Prof

Dept. of ISE, PESIT

09 CS 303

Compiler Design
-
Laboratory Manual.

For 5th Semester students.

By

(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.

Dept of IS&E, PESIT

2

Lex and Yacc

1.

Write a lex Program to identify a simple and a compound

statement
.

%{

#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()

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

3

{

printf(“
\
n Enter a sentence: “);

yylex();

if(error==1 || F2==0 || F1==0)

{

printf(“
\
n Invalid sentence”);

exit(0);

}

if(F0==1 && l1==1 && l2==1)

printf(“
\
n Compound sentence
\
n”);

else

printf(”
\
nSimple sentence
\
n”);

}

Aliter

%{

#include<stdio.h>

int flag=0;

%}

%%

if|

because|

or|

either|

nor|

neither|

but{flag=1;}}

%%

main()

{

printf("Enter the sentence");

yylex();

if(flag)

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

4

{

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);

}

3.

Write a lex program to convert an octal number to

decimal number.

%{

#include<stdio.h>

Void octtodeci(int);

%}

%%

[0
-
7]+{octtodeci(atoi(yytext));}

%%

int main()

{

Printf(“
\
n enter the octal number”);

Yylex();

}

Void octtodeci(int num
)

{

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

5

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;

}

Printf(“
\
n the decimal equivalent is %d”,sum);

}

int po(int a

,

int m)

{

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

|

;

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

6

%%

yyerror()

{

Printf(“
\
n the string does not belong to the grammar”);

}

int yylex()

{

char ch;

ch=getchar();

if (ch==’a’)

return A;

else if (ch==’b’)

return B;

else if(ch==’
\
n’)

return 0;

else return ch;

}

int main()

{

Printf(“enter the expression
\
n”);

yyparse();

Printf(“
\
n string accepted by grammar”);

Return 0;

}

5.

Write a YACC program to check the validity of an arithmetic expression.

%{

#include<stdio.h>

#include<stdlib.h>

%}

%token ID

%left '+','
-
'

%left '*','/'

%%

E:E'+'E

{ printf("valid expression);}

|

E'
-
'E

{ printf("valid expression);}

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

7

|

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;

else return ch;

}

yyerror()

{

Printf(“
\
n invalid expression”);

}

int main()

{

printf("Enter the expression");

yyparse()
;

}

6.

Write a YACC Program to identify an input for the grammar a^nb (n>=10)

/* 6.l
lex part
*/

%{

#include<stdio.h>

#include “y.tab.h”

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

8

%}

%%

[aA] return A;

[bB] return B;

.|
\
n return
yytext[0];

%%

—————————————

6.y

/* 6.y

yacc part

*/

%{

#include<stdio.h>

%}

%token A B

%%

stmt: S ‘
\
n’ {printf(“
\
nValid expression”);

exit(1);}

;

S: X B

X: X A | AAAAAAAAA
A

%%

main()

{

printf(“
\
nEnter the expression: “);

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

9

if(yyparse())

{

printf(“
\
nValid

expression”);

exit(0);

}

}

int yyerror()

{

printf(“
\
nInvalid expression
\
n”);

return 1;

}

int yywrap()

{

return 1;

}

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

10

ANTLR(
Another tool for

Language Recognition
).

In computer
-
based language recognition, ANTLR (pronounced Antler), or
Another

Tool for
Language Recognition, is a parser generator that uses
LL (
*) parsing. ANTLR is the successor to
the Purdue Compiler Construction Tool Set (PCCTS), first developed in 1989, and is under
active development. Its maintainer is
Professor

Terence Parr of the University of San Francisco.

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
a95, 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 combi
ned 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 parse
r/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 n
ot 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 aut
omatically generated by parsers.
These tree parsers are unique to ANTLR and greatly simplify the processing of abstract syntax
trees.

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

11

How to install

and use

ANTLR?

Open eclipse ide.

Select File

New

JPA Project.

Ente
r container name, project name, ok or finish.

Select File

New

java project.

Enter a project name

ok.

If there is a prompt that asks for “build nature” press ok.

Right click on the project in the package explorer.

New

other

ANTLR

combined grammar

next

enter

grammar name (grammar
name = class name)

ok.

The ide generates a .g file

for you.

Type the program in the text editor.

Right click on the project

in the package explorer.

Select

configure

ANTLR project.

Right click on the project again in the package
explorer.

Select Build path

Select the directory where the ANTLR jar file resides.

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.

If build fails, go to run

run as

java application

ok

Select Tool
-
org.antlr

ok, this

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

12

1.

Write an ANTLr grammar to accept the pascal statement READ(Value) and print a
parse tree for the same.

options {

language = Java;

}

tokens{

OB = '(';

CB = ')';

CM = ',';

}

@members

{

/*public static void
main (String [
] args)

throws Exception

{

CommonTokenStream tokens = new CommonTokenStream(lex);

try

{

parser.expr();

}

catch(RecognitionException e)

{

e.printStackTrace();

}

}*/

}

IDLIST : ID(((CM)ID)+)?;

fragment ID : AL+;

fragment AL : 'a'..'z'|'A'..'Z';

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

13

2.

Write an ANTLr grammar to perform basic arithmetic operation in a calculator

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;

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

14

3.

Write an ANTLr grammar to accept a block of PASCAL statements
between begin
and end
and print the parse tree for the
same.

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)+)?;

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

15

4.

Write an ANTLr grammar to decide whether given sentence is simple or compound
.

grammar Prog2;

options

{

language = Java;

}

VERBS : 'doing' | 'using';

NOUN : 'bangalore' | 'dog';

sente

Compiler Design
-
Laboratory Manual.

Dept of IS&E, PESIT

16

09 CS 303

Week #

Program #

List of Programs

1

Instruction
class.

Introduction to the lab procedures and evaluation scheme, Lex and YACC
specifications.

2

Program #1

Write a lex Program to identify a simple and a compound statement.

3

Program #2

Write a lex Program to
count the number of keywords and identifiers in a sentence.

4

Program #3

Write a lex program to convert an octal number to decimal number.

5

Program #4

Write a YACC Program to check whether given string a^nb^n is accepted by the
grammar.

6

Program #5

Write a YACC program to check the validity of an arithmetic expression.

7

Program #6

Write a YACC Program to identify an input for the grammar a^nb (n>=10).

8

Instruction
class

Instruction class on
ANTLR (Another tool for Language Recognition).

9

Program #1

Write an ANTLr grammar to accept the pascal statement READ(Value) and print a
parse tree for the same.

10

Program #2

Write an ANTLr grammar to perform basic arithmetic operation in a calculator

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.

Compiler Design
-
Laboratory Manual.