The left recursion from a grammar is removed in the left_recursion.py
to run the program :- $python left_recursion.py
length3
keyE
number of production2
E+F
F
keyF
number of production2
FG
G
keyG
number of production2
(G)
i
{'E': ['E+F', 'F'], 'G': ['(G)', 'i'], 'F': ['FG', 'G']}
left recursion found
left recursion found
{"E'": ['', "+FE'"], "F'": ['', "*GF'"], 'E': "FE'", 'G': ['(G)', 'i'], 'F': "GF'"}
The obtained output is left recursion free.
The left factoring is removed in left_fact.py
to run the program :- $python left_fact.py
length01
keyE
number of production4
abAB
abCD
abEF
ab
the given grammar grammar
{'E': ['abAB', 'abCD', 'abEF', 'ab']}
removing left factoring free grammer
{"E'": ['ab'], 'E': ['AB', 'CD', 'EF', '']}
The obtained grammar is left factoring free.
lexical analyser is bulid using using PLY python programing language and lex tool.
lexical_lex.py contain python implemented code of lex, it will list out all the token present in sum.c
to run the program :- $python lexical_lex.py
LexToken(INCLUDE,'#include',1,0)
LexToken(LESSER,'<',1,8)
LexToken(ID,'stdio',1,9)
Illegal character '.'
LexToken(ID,'h',1,15)
LexToken(GREATER,'>',1,16)
LexToken(VOID,'void',2,18)
LexToken(FUNCTION,'main(',2,23)
LexToken(RPAREN,')',2,28)
LexToken(LBRAC,'{',3,30)
LexToken(INT,'int',5,43)
LexToken(ID,'a',5,47)
LexToken(COMMA,',',5,48)
LexToken(ID,'b',5,49)
LexToken(COLON,';',5,50)
LexToken(PRINTF,'printf(',6,52)
LexToken(RPAREN,')',6,77)
LexToken(COLON,';',6,78)
LexToken(SCANF,'scanf(',7,80)
LexToken(COMMA,',',7,92)
Illegal character '&'
LexToken(ID,'a',7,94)
LexToken(COMMA,',',7,95)
Illegal character '&'
LexToken(ID,'b',7,97)
LexToken(RPAREN,')',7,98)
LexToken(COLON,';',7,99)
LexToken(INT,'int',8,101)
LexToken(ID,'s',8,105)
LexToken(EQUAL,'=',8,107)
LexToken(ID,'a',8,109)
LexToken(PLUS,'+',8,110)
LexToken(ID,'b',8,111)
LexToken(COLON,';',8,112)
LexToken(PRINTF,'printf(',9,114)
LexToken(COMMA,',',9,132)
LexToken(ID,'s',9,133)
LexToken(RPAREN,')',9,134)
LexToken(COLON,';',9,135)
LexToken(RBRAC,'}',10,137)
The program first.py will calculate first the program will automatically remove all left factoring and left recursion in the grammar. the ll1table.py contain first, follow and ll1 table in the code. the code is broken down into the different different function.
A basic calculator is designed using the grammar.
the calculator perform addition, subtraction, multiplication , divison and negation of the numbers in the equation
cal_lex.l is the lex code lexically analyse the grammar
cal_yac.y is the yacc code to sematically analyse the grammar
command to run the code:-
$lex cal_lex.l
$yacc cal_yac.y
$gcc y.tab.c -ly -lfl -lm
$./a.out
Enter the expression: 2+3+4
Answer: 9
Enter:
assignement.l is the lex code written to perform five task:-
- count the word start with vowel along with position of word.
- count the word start with consonats along with position of word.
- count the article along with position.
- count the numbers along with position.
- count number of line in file.