Giter VIP home page Giter VIP logo

compiler-programs's Introduction

Left Recursion

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': ['F
G', '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.

Left Factoring

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

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)

First, Follow and LL1 table

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.

Calculator design using lex and yacc

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:

Other program

assignement.l is the lex code written to perform five task:-

  1. count the word start with vowel along with position of word.
  2. count the word start with consonats along with position of word.
  3. count the article along with position.
  4. count the numbers along with position.
  5. count number of line in file.

compiler-programs's People

Contributors

raghav714 avatar sraj-git avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.