Académique Documents
Professionnel Documents
Culture Documents
WRITTEN IN C LANGUAGE
A SYNOPSIS
Minor Project Submitted in partial fulfillment of the requirement for Degree of
BACHELOR OF ENGINEERING
IN
COMPUTER SCIENCE &ENGINEERING
RA1IV GANDHI PRODYOGIKI VISHWAVIDYALAYA, BHOPAL (M.P.)]
Submitted By: Guided By:
Swati Kiyawat (0101CS011!"# Prof$%aji& pandey
%i'hi Pari(h (0101CS010!# Prof$Shi(ha )garwal
)lo( Sharma (0101*+0100,#
DEPARTMENT OF COMPUTER SCIENCE
UNIVERSITY INSTITUTE OF TECHNOLOGY
RA1IV GANDHI PRODYOGIKI VISHWAVIDYALYA, BHOPAL
April 2012
Rajiv Gandhi Prodyogiki Vishwavidyalaya, Bhopal
(M.P.)
Department of Computer Science & Engineering
CERTIFICATE
+hi' i' to certify that the wor( embodied in thi' 'ynop'i' entitled
~Lexical Analysis and Parser Written in C Language being
'ubmitted by ~Swati kiyawat (Roll No.: 0101CS091123) , ~Rishi
Parikh (Roll No.: 0101CS091092) & ~Alok Sharma (Roll No.:
0101IT091004) for partial fulfillment of the requirement for the degree of
~Bachelor of Engineering in Computer Science & Engineering
di'cipline to -%aji& .andhi Praudyogi(i /i'hwa&idyalaya0
1hopal(M$P$#2 during the academic year !01! i' a record of bona fide
piece of wor(0 carried out by him under my 'uper&i'ion and guidance in
the ~Department of Computer Science & Engineering, University
Institute of Technology, Bhopal (M.P.).
APPROVED & GUIDED BY:
Prof. Rajiv Pandey
Prof.Shikha Agarwal
(Profe''or0 Department 3f Computer Science 4 5ngineering#
ii
University Institute of Technology, Bhopal (M.P.)
Department Of Computer Science & Engineering
DECLARATION
6e Swati Kiyawat, Rishi Parikh and Alok Sharma0 'tudent' of
Bachelor of Engineering in Computer Science di'cipline0
session: 2012, University Institute of Technology-Bhopal
(M.P.), here by declare that the wor( pre'ented in thi' 'ynop'i'
entitled ~Lexical Analysis and Parser Written in C Language
i' the outcome of our own wor(0 i' bona fide and correct to the be't
of our (nowledge and thi' wor( ha' been carried out ta(ing care of
5ngineering 5thic'$ +he wor( pre'ented doe' not infringe any
patented wor( and ha' not been 'ubmitted to any other uni&er'ity
or anywhere el'e for the award of any degree or any profe''ional
diploma$
Swati Kiyawat
Date7 5nrollment 8o$7 0101CS011!"
%i'hi Pari(h
5nrollment 8o$7 0101CS010!
)lo( Sharma
5nrollment 8o$7 0101*+0100,
iii
CONTENTS
TOPIC NAME PAGE NO.
Certificate i
Declaration ii
)b'tract 1
1$0 *8+%3D9C+*38 2
1.1.0 Introduction to Lexical Grammar 3
1.2.0 Introduction to Token 4
1.3.0 How scanner and tokenizer works ? 4
1.4.0 Platorm used 7
!$0 P%3P3S5D M5+:3D3;3.< 9
2.1.0 !lock "ia#ram 9
2.2.0 "ata $low "ia#ram 9
2.3.0 $low %&art 11
"$0 )PP%3)C:5D %5S9;+ )8D C38C;9S*38 13
,$0 )PP;*C)+*38S )8D =9+9%5 63%K 14
%5=5%58C5S 15
i&
ABSTRACT
+he le>ical analy?er i' re'pon'ible for 'canning the 'ource input file and tran'lating
le>eme' ('tring'# into 'mall object' that the compiler for a high le&el language can
ea'ily proce''$ +he'e 'mall &alue' are often called -to(en'2$ +he le>ical analy?er i'
al'o re'pon'ible for con&erting 'equence' of digit' in to their numeric form a' well a'
proce''ing other literal con'tant'0 for remo&ing comment' and white 'pace' from the
'ource file0 and for ta(ing care of many other mechanical detail'$ ;e>ical analy?er
con&ert' 'tream of input character' into a 'tream of to(en'$ =or to(eni?ing into
identifier' and (eyword' we incorporate a 'ymbol table which initially con'i't' of
predefined (eyword'$ +he to(en' are read from an input file$ +he output file will
con'i't of all the to(en' pre'ent in our input file along with their re'pecti&e to(en
&alue'$
KEYWORDS: ;e>eme0 ;e>ical )naly'i'0 Compiler0 Par'er0 +o(en
1
1.0 INTRODUCTION
Compiler and its phases:
) compiler i' a program ta(e' a program written in a 'ource language and tran'late' it
into an equi&alent program in a target language.*t i' a program that tran'late' a high@
le&el language into a functionally equi&alent low@le&el language program$ So0a
compiler i' ba'ically a tran'lator who'e 'ource language i' high le&el language0and
target language i' low@le&el language0 that i'0 a compiler i' u'ed to implement a high
le&el language on a computer$
*n computer 'cience0 lexical analysis i' the proce'' of con&erting a 'equence of
character' into a 'equence of to(en'$ ) program or function which perform' le>ical
analy'i' i' called a lexical analyzer0 lexer or scanner$ ) le>er often e>i't' a' a 'ingle
function which i' called by a par'er or another function and wor(' along'ide other
component' for ma(ing compilation of a high le&el language po''ible$ +hi' complete
'etup i' what we call a compiler$
+o define what a compiler i' one mu't fir't define what a tran'lator i'$ ) tran'lator
i' a program that ta(e' another program written in one language0 al'o (nown a' the
'ource language0 and output' a program written in another language0 (nown a' the
target language$
8ow that the tran'lator i' defined0 a compiler can be defined a' a tran'lator$ +he
'ource language i' a high@le&el language 'uch a' Aa&a or Pa'cal and the target
language i' a low@le&el language 'uch a' machine or a''embly$
+here are fi&e part' of compilation (or pha'e' of the compiler#
1$# ;e>ical )naly'i'
!$# Synta> )naly'i'
"$# Semantic )naly'i'
,$# Code 3ptimi?ation
B$# Code .eneration
;e>ical )naly'i' i' the act of ta(ing an input 'ource program and outputting a
'tream of to(en'$ +hi' i' done with the Scanner$ +he Scanner can al'o place
identifier' into 'omething called the 'ymbol table or place 'tring' into the 'tring table$
+he Scanner can report tri&ial error' 'uch a' in&alid character' in the input file$
!
Synta> )naly'i' i' the act of ta(ing the to(en 'tream from the 'canner and
comparing them again't the rule' and pattern' of the 'pecified language$ Synta>
)naly'i' i' done with the Par'er$ +he Par'er produce' a tree0 which can come in many
format'0 but i' referred to a' the par'e tree$ *t report' error' when the to(en' do not
follow the s'ntax of the 'pecified language$ 5rror' that the Par'er can report are
'yntactical error' 'uch a' mi''ing parenthe'i'0 'emicolon'0 and (eyword'$
Semantic )naly'i' i' the act of determining whether or not the par'e tree i'
rele&ant and meaningful$ +he output i' intermediate code0 al'o (nown a' an
intermediate repre'entation (or *%#$ Mo't of the time0 thi' *% i' clo'ely related to
a''embly language but it i' machine independent$ *ntermediate code allow' different
code generator' for different machine' and promote' ab'traction and portability from
'pecific machine time' and language'$ (* dare to 'ay that the mo't famou' e>ample i'
ja&aC' byte@code and A/M#$ Semantic )naly'i' find' more meaningful error' 'uch a'
undeclared &ariable'0 type compatibility0 and 'cope re'olution$
Code 3ptimi?ation ma(e' the *% more efficient$ Code optimi?ation i' u'ually
done in a 'equence of 'tep'$ Some optimi?ation' include code ho'ting0 or mo&ing
con'tant &alue' to better place' within the code0 redundant code di'co&ery0 and
remo&al of u'ele'' code$
Code .eneration i' the final 'tep in the compilation proce''$ +he input to the
Code .enerator i' the *% and the output i' machine language code$
1.1.0 Introduction to Lexical Grammar
+he 'pecification of a programming language will often include a 'et of rule'
which define' the le>er$ +he'e rule' are u'ually called regular e>pre''ion' and they
define the 'et of po''ible character 'equence' that are u'ed to form to(en'
or le>eme'$ white'pace0 (i$e$ character' that are ignored#0 are al'o defined in
the regular e>pre''ion'$
"
1.2.0 Introduction to token
) token i' a 'tring of character'0 categori?ed according to the rule' a' a 'ymbol
(e$g$ *D58+*=*5%0 89M15%0 C3MM)0 etc$#$ +he proce'' of forming to(en' from
an input 'tream of character' i' called (tokenization# and the le>er categori?e' them
according to a 'ymbol type$ ) to(en can loo( li(e anything that i' u'eful for
proce''ing an input te>t 'tream or te>t file$
) le>ical analy?er generally doe' nothing with combination' of to(en'0 a ta'( left
for a par'er$ =or e>ample0 a typical le>ical analy?er recogni?e' parenthe'i' a' to(en'0
but doe' nothing to en'ure that each D(D i' matched with a D#D$
Con'ider thi' e>pre''ion in the C programming language7
'umE"F!G
+o(eni?ed in the following table7
+o(en' are frequently defined by regular e>pre''ion'0 which are under'tood by a
le>ical analy?er generator 'uch a' le>$ +he le>ical analy?er (either generated
automatically by a tool li(e le>0 or hand@crafted# read' in a 'tream of character'0
identifie' the le>eme' in the 'tream0 and categori?e' them into to(en'$ +hi' i' called
Hto(eni?ing$H *f the le>er find' an in&alid to(en0 it will report an error$
=ollowing to(eni?ing i' par'ing$ =rom there0 the interpreted data may be loaded
into data 'tructure' for general u'e0 interpretation0 or compiling$
lex
eme
token
type
Su
m
*dentifier
E
)''ignme
nt operator
" 8umber
F
)ddition
operator
! 8umber
G
5nd of
'tatement
,
1.3.0 How scanner and tokenizer work?
+he fir't 'tage0 the scanner0 i' u'ually ba'ed on a finite 'tate machine$ *t ha'
encoded within it information on the po''ible 'equence' of character' that can be
contained within any of the to(en' it handle' (indi&idual in'tance' of the'e character
'equence' are (nown a' le>eme'#$ =or in'tance0 an inte#er to(en may contain any
'equence of numerical digit character'$ *n many ca'e'0 the fir't non@white 'pace
character can be u'ed to deduce the (ind of to(en that follow' and 'ub'equent input
character' are then proce''ed one at a time until reaching a character that i' not in the
'et of character' acceptable for that to(en (thi' i' (nown a' the ma>imal munch rule0
or longe't match rule#$ *n 'ome language' the le>eme creation rule' are more
complicated and may in&ol&e bac(trac(ing o&er pre&iou'ly read character'$
Tokenization i' the proce'' of demarcating and po''ibly cla''ifying 'ection' of a
'tring of input character'$ +he re'ulting to(en' are then pa''ed on to 'ome other form
of proce''ing$ +he proce'' can be con'idered a 'ub@ta'( of par'ing input$
+a(e0 for e>ample0 the following 'tring$
+he quic( brown fo> jump' o&er the la?y dog
9nli(e human'0 a computer cannot intuiti&ely D'eeD that there are word'$ +o a
computer thi' i' only a 'erie' of ," character'$
A process of tokenization could be used to split the sentence into word
tokens. Although the following exaple is gi!en as "#$ there are an%
wa%s to represent tokenized input&
I'entenceJ
IwordJ+heIKwordJ
IwordJquic(IKwordJ
IwordJbrownIKwordJ
IwordJfo>IKwordJ
IwordJjump'IKwordJ
IwordJo&erIKwordJ
IwordJtheIKwordJ
IwordJla?yIKwordJ
IwordJdogIKwordJ
IK'entenceJ
B
) le>eme0 howe&er0 i' only a 'tring of character' (nown to be of a certain (ind
(eg0 a 'tring literal0 a 'equence of letter'#$ *n order to con'truct a to(en0 the le>ical
analy?er need' a 'econd 'tage0 the evaluator0 which goe' o&er the character' of the
le>eme to produce a (alue$ +he le>emeD' type combined with it' &alue i' what
properly con'titute' a to(en0 which can be gi&en to a par'er$ (Some to(en' 'uch a'
parenthe'e' do not really ha&e &alue'0 and 'o the e&aluator function for the'e can
return nothing$ +he e&aluator' for integer'0 identifier'0 and 'tring' can be con'iderably
more comple>$ Sometime' e&aluator' can 'uppre'' a le>eme entirely0 concealing it
from the par'er0 which i' u'eful for white'pace and comment'$#
=or e>ample0 in the 'ource code of a computer program the 'tring
netLworthLfuture E (a''et' @ liabilitie'#G
might be con&erted (with white'pace 'uppre''ed# into the le>ical to(en 'tream7
+hough it i' po''ible and 'ometime' nece''ary to write a le>er by hand0 le>er' are
often generated by automated tool'$ +he'e tool' generally accept regular e>pre''ion'
that de'cribe the to(en' allowed in the input 'tream$ 5ach regular e>pre''ion i'
a''ociated with a production in the le>ical grammar of the programming language that
e&aluate' the le>eme' matching the regular e>pre''ion$ +he'e tool' may generate
'ource code that can be compiled and e>ecuted or con'truct a 'tate table for a finite
'tate machine (which i' plugged into template code for compilation and e>ecution#$
%egular e>pre''ion' compactly repre'ent pattern' that the character' in le>eme'
might follow$ =or e>ample0 for an 5ngli'h@ba'ed language0 a 8)M5 to(en might be
any 5ngli'h alphabetical character or an under'core0 followed by any number of
NAME "net_worth_future"
EQUALS
OPEN_PARENTHESIS
NAME "assets"
MINUS
NAME "liabilities"
LOSE_PARENTHESIS
SEMIOLON
M
in'tance' of any )SC** alphanumeric character or an under'core$ +hi' could be
repre'ented compactly by the 'tring Na@?)@OLPNa@?)@OL0@PQ$ +hi' mean' Hany
character a@?0 )@O or L0 followed by 0 or more of a@?0 )@O0 L or 0@H$
%egular e>pre''ion' and the finite 'tate machine' they generate are not powerful
enough to handle recur'i&e pattern'0 'uch a' Hn opening parenthe'e'0 followed by a
'tatement0 followed by n clo'ing parenthe'e'$H +hey are not capable of (eeping count0
and &erifying that n i' the 'ame on both 'ide' R unle'' you ha&e a finite 'et of
permi''ible &alue' for n$ *t ta(e' a full@fledged par'er to recogni?e 'uch pattern' in
their full generality$ ) par'er can pu'h parenthe'e' on a 'tac( and then try to pop
them off and 'ee if the 'tac( i' empty at the end$
+he ;e> programming tool and it' compiler i' de'igned to generate code for fa't
le>ical analy'er' ba'ed on a formal de'cription of the le>ical 'ynta>$ *t i' not generally
con'idered 'ufficient for application' with a complicated 'et of le>ical rule' and
'e&ere performance requirement'G for in'tance0 the .89 Compiler Collection u'e'
hand@written le>er'$
1.4.0 Platform used
*n computing0 C i' a general@purpo'e computer programming language originally
de&eloped in 1S! by Denni' %itchie at the 1ell +elephone ;aboratorie' to implement
the 9ni> operating 'y'tem$
)lthough C wa' de'igned for writing architecturally independent 'y'tem 'oftware0
it i' al'o widely u'ed for de&eloping application 'oftware$
6orldwide0 C i' the fir't or 'econd mo't popular language in term' of number of
de&eloper po'ition' or publicly a&ailable code$ *t i' widely u'ed on many different
'oftware platform'0 and there are few computer architecture' for which a C compiler
doe' not e>i't$ C ha' greatly influenced many other popular programming language'0
mo't notably CFF0 which originally began a' an e>ten'ion to C0 and Aa&a and CT
which borrow C le>ical con&ention' and operator'$
i. %&aracteristics
S
;i(e mo't imperati&e language' in the );.3; tradition0 C ha' facilitie' for
'tructured programming and allow' le>ical &ariable 'cope and recur'ion0 while a 'tatic
type 'y'tem pre&ent' many unintended operation'$ *n C0 all e>ecutable code i'
contained within function'$ =unction parameter' are alway' pa''ed by &alue$ Pa''@by@
reference i' achie&ed in C by e>plicitly pa''ing pointer &alue'$ :eterogeneou'
aggregate data type' ('truct# allow related data element' to be combined and
manipulated a' a unit$ C program 'ource te>t i' free@format0 u'ing the 'emicolon a' a
'tatement terminator (not a delimiter#$
C al'o e>hibit' the following more 'pecific characteri'tic'7
non@ne't able function definition'
&ariable' may be hidden in ne'ted bloc('
partially wea( typingG for in'tance0 character' can be u'ed a' integer'
low@le&el acce'' to computer memory by con&erting machine addre''e' to
typed pointer'
function and data pointer' 'upporting ad &oc run@time polymorphi'm
array inde>ing a' a 'econdary notion0 defined in term' of pointer arithmetic
a preproce''or for macro definition0 'ource code file inclu'ion0 and conditional
compilation
comple> functionality 'uch a' *K30 'tring manipulation0 and mathematical
function' con'i'tently delegated to library routine'
) relati&ely 'mall 'et of re'er&ed (eyword' (originally "!0 now "S in C#
) large number of compound operator'0 'uch a' !"0 !!
ii. $eatures
+he relati&ely low@le&el nature of the language afford' the programmer clo'e
control o&er what the computer doe'0 while allowing 'pecial tailoring and aggre''i&e
optimi?ation for a particular platform$ +hi' allow' the code to run efficiently on &ery
limited hardware0 'uch a' embedded 'y'tem'$
iii. Tur)o %**
+urbo CFF i' a CFF compiler and integrated de&elopment en&ironment (*D5#
from 1orland$ +he original +urbo CFF product line wa' put on hold after 1,0 and
wa' re&i&ed in !00M a' an introductory@le&el *D50 e''entially a 'tripped@down &er'ion
of their flag'hip CFF 1uilder$ +urbo CFF !00M wa' relea'ed on September B0 !00M
and i' a&ailable in D5>plorerD and DProfe''ionalD edition'$ +he 5>plorer edition i' free to
U
download and di'tribute while the Profe''ional edition i' a commercial product$ +he
profe''ional edition i' no longer a&ailable for purcha'e from 1orland$
+urbo CFF "$0 wa' relea'ed in 11 ('hipping on 8o&ember !0#0 and came in
amid't e>pectation' of the coming relea'e of +urbo CFF for Micro'oft 6indow'$
*nitially relea'ed a' an MS@D3S compiler0 "$0 'upported CFF template'0 1orlandD'
inline a''embler0 and generation of MS@D3S mode e>ecutable' for both U0UM real@
mode 4 !UM@protected (a' well a' the *ntel U01UM$# "$0D' implemented )+4+ CFF
!$10 the mo't recent at the time$ +he 'eparate +urbo )''embler product wa' no longer
included0 but the inline@a''embler could 'tand in a' a reduced functionality &er'ion$
2.0 PROPOSED METHODOLOGY
)im of the project i' to de&elop a ;e>ical )naly?er that can generate to(en' for the
further proce''ing of compiler$ +he job of the le>ical analy?er i' to read the 'ource
program one character at a time and produce a' output a 'tream of to(en'$ +he to(en'
produced by the le>ical analy?er 'er&e a' input to the ne>t pha'e0 the par'er$ +hu'0 the
le>ical analy?erC' job i' to tran'late the 'ource program in to a form more conducti&e
to recognition by the par'er$
+he goal of thi' program i' to create to(en' from the gi&en input 'tream$
2.1.0 Block diagram
INPUT
SOURCE
IN C
TOKENIZED
OUTPUT
FILE
LEXICAL
ANALYSER
CODE
rules ]
UB
programs ]
+he definition 'ection contain' information about to(en'0 data type'0 and
grammar rule'$ *t al'o include' any C code that mu't go directly into the
output file at it' beginning$
Parser Program:
Wto(en 89M
W'tart S
WW
S75 Xprintf(Hthe &alue of e>pre''ion i' WdZnH0a1#GY
G
575DFD+ XaaEa1Fa"GY
G
57+ XaaEa1GY
G
+7+DQD= XaaEa1Qa"GY
G
UM
+7= XaaEa1GY
G
=789M XaaEa1GY
G
WW
TincludeHle>$yy$cH
main(#
X
return(yypar'e(##G
Y
lex.yy.c file of parser:
D*.*+ N0@P
WW
XD*.*+YF Xyyl&alEatoi(yyte>t#G
return(89M#G
Y
NZtPG
return(yyte>tN0P#G
US
y.tab.c file:
Tifndef lint
'tatic con't char yy'cc'idNP E H`(T#yaccpar 1$ (1er(eley# 0!K!1K"HG
Tendif
Tinclude I'tdlib$hJ
Tinclude I'tring$hJ
Tdefine <<1<)CC 1
Tdefine <<M)A3% 1
Tdefine <<M*83%
Tdefine <<P)+C: !00S0B0
Tdefine <<5MP+< (@1#
Tdefine yyclearin (yychar E <<5MP+<#
Tdefine yyerro( (yyerrflag E 0#
Tdefine <<%5C3/5%*8. (yyerrflag \E 0#
e>tern int yypar'e(&oid#G
'tatic int yygrow'tac((&oid#G
Tdefine <<P%5=*] HyyH
Tdefine 89M !BS
Tdefine <<5%%C3D5 !BM
'hort yylh'NP E X @10
00 10 10 !0 !0 "0
YG
'hort yylenNP E X !0
10 "0 10 "0 10 10
YG
'hort yydefredNP E X 00
M0 00 00 00 B0 00 00 00 ,0
YG
'hort yydgotoNP E X !0
UU
"0 ,0 B0
YG
'hort yy'inde>NP E X @!BS0
00 00 @,00 @"U0 00 @!BS0 @!BS0 @"U0 00
YG
'hort yyrinde>NP E X 00
00 00 B0 10 00 00 00 !0 00
YG
'hort yyginde>NP E X 00
00 "0 @10
YG
Tdefine <<+)1;5S*O5 ,B
'hort yytableNP E X 10
"0 !0 M0 S0 10 0 00 00 U0 00
00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00
00 00 00 "0 !0
YG
'hort yychec(NP E X !BS0
00 00 ,"0 ,!0 00 S0 @10 @10 M0 @10
@10 @10 @10 @10 @10 @10 @10 @10 @10 @10
@10 @10 @10 @10 @10 @10 @10 @10 @10 @10
@10 @10 @10 @10 @10 @10 @10 @10 @10 @10
@10 @10 @10 ,"0 ,"0
YG
Tdefine <<=*8); !
Tifndef <<D519.
Tdefine <<D519. 0
Tendif
Tdefine <<M)]+3K58 !BS
Tif <<D519.
char QyynameNP E X
Hend@of@fileH0000000000000000000000000000000000000000000000000000000000000000000
U
0000000000000000HDQDH0HDFDH00000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000H89MH0
YG
char QyyruleNP E X
Haaccept 7 SH0
HS 7 5H0
H5 7 5 DFD +H0
H5 7 +H0
H+ 7 + DQD =H0
H+ 7 =H0
H= 7 89MH0
YG
Tendif
Tifndef <<S+<P5
typedef int <<S+<P5G
Tendif
Tif <<D519.
Tinclude I'tdio$hJ
Tendif
KQ define the initial 'tac(@'i?e' QK
Tifdef <<S+)CKS*O5
Tundef <<M)]D5P+:
Tdefine <<M)]D5P+: <<S+)CKS*O5
Tel'e
Tifdef <<M)]D5P+:
Tdefine <<S+)CKS*O5 <<M)]D5P+:
Tel'e
Tdefine <<S+)CKS*O5 B00
Tdefine <<M)]D5P+: B00
0
Tendif
Tendif
Tdefine <<*8*+S+)CKS*O5 B00
int yydebugG
int yynerr'G
int yyerrflagG
int yycharG
'hort Qyy''pG
<<S+<P5 Qyy&'pG
<<S+<P5 yy&alG
<<S+<P5 yyl&alG
KQ &ariable' for the par'er 'tac( QK
'tatic 'hort Qyy''G
'tatic 'hort Qyy''limG
'tatic <<S+<P5 Qyy&'G
'tatic int yy'tac('i?eG
Tline 1S Hte't$yH
TincludeHle>$yy$cH
main(#
X
return(yypar'e(##G
Y
Tline 1! Hy$tab$cH
KQ allocate initial 'tac( or double 'tac( 'i?e0 up to <<M)]D5P+: QK
'tatic int yygrow'tac((&oid#
X
int new'i?e0 iG
'hort Qnew''G
<<S+<P5 Qnew&'G
1
if ((new'i?e E yy'tac('i?e# EE 0#
new'i?e E <<*8*+S+)CKS*O5G
el'e if (new'i?e JE <<M)]D5P+:#
return @1G
el'e if ((new'i?e QE !# J <<M)]D5P+:#
new'i?e E <<M)]D5P+:G
i E yy''p @ yy''G
new'' E (yy'' \E 0#
_ ('hort Q#realloc(yy''0 new'i?e Q 'i?eof(Qnew''##
7 ('hort Q#malloc(new'i?e Q 'i?eof(Qnew''##G
if (new'' EE 0#
return @1G
yy'' E new''G
yy''p E new'' F iG
new&' E (yy&' \E 0#
_ (<<S+<P5 Q#realloc(yy&'0 new'i?e Q 'i?eof(Qnew&'##
7 (<<S+<P5 Q#malloc(new'i?e Q 'i?eof(Qnew&'##G
if (new&' EE 0#
return @1G
yy&' E new&'G
yy&'p E new&' F iG
yy'tac('i?e E new'i?eG
yy''lim E yy'' F new'i?e @ 1G
return 0G
Y
Tdefine <<)13%+ goto yyabort
Tdefine <<%5A5C+ goto yyabort
Tdefine <<)CC5P+ goto yyaccept
Tdefine <<5%%3% goto yyerrlab
int
!
yypar'e(&oid#
X
regi'ter int yym0 yyn0 yy'tateG
Tif <<D519.
regi'ter con't char Qyy'G
if ((yy' E geten&(H<<D519.H## \E 0#
X
yyn E Qyy'G
if (yyn JE D0D 44 yyn IE DD#
yydebug E yyn @ D0DG
Y
Tendif
yynerr' E 0G
yyerrflag E 0G
yychar E <<5MP+<G
if (yy'' EE 89;; 44 yygrow'tac((## goto yyo&erflowG
yy''p E yy''G
yy&'p E yy&'G
Qyy''p E yy'tate E 0G
yyloop7
if ((yyn E yydefredNyy'tateP# \E 0# goto yyreduceG
if (yychar I 0#
X
if ((yychar E yyle>(## I 0# yychar E 0G
Tif <<D519.
if (yydebug#
X
yy' E 0G
if (yychar IE <<M)]+3K58# yy' E yynameNyycharPG
if (\yy'# yy' E Hillegal@'ymbolHG
"
printf(HW'debug7 'tate Wd0 reading Wd (W'#ZnH0
<<P%5=*]0 yy'tate0 yychar0 yy'#G
Y
Tendif
Y
if ((yyn E yy'inde>Nyy'tateP# 44 (yyn FE yychar# JE 0 44
yyn IE <<+)1;5S*O5 44 yychec(NyynP EE yychar#
X
Tif <<D519.
if (yydebug#
printf(HW'debug7 'tate Wd0 'hifting to 'tate WdZnH0
<<P%5=*]0 yy'tate0 yytableNyynP#G
Tendif
if (yy''p JE yy''lim 44 yygrow'tac((##
X
goto yyo&erflowG
Y
QFFyy''p E yy'tate E yytableNyynPG
QFFyy&'p E yyl&alG
yychar E <<5MP+<G
if (yyerrflag J 0# @@yyerrflagG
goto yyloopG
Y
if ((yyn E yyrinde>Nyy'tateP# 44 (yyn FE yychar# JE 0 44
yyn IE <<+)1;5S*O5 44 yychec(NyynP EE yychar#
X
yyn E yytableNyynPG
goto yyreduceG
Y
if (yyerrflag# goto yyinreco&eryG
yyerror(H'ynta> errorH#G
Tifdef lint
,
goto yyerrlabG
Tendif
yyerrlab7
FFyynerr'G
yyinreco&ery7
if (yyerrflag I "#
X
yyerrflag E "G
for (GG#
X
if ((yyn E yy'inde>NQyy''pP# 44 (yyn FE <<5%%C3D5# JE 0 44
yyn IE <<+)1;5S*O5 44 yychec(NyynP EE <<5%%C3D5#
X
Tif <<D519.
if (yydebug#
printf(HW'debug7 'tate Wd0 error reco&ery 'hiftingZ
to 'tate WdZnH0 <<P%5=*]0 Qyy''p0 yytableNyynP#G
Tendif
if (yy''p JE yy''lim 44 yygrow'tac((##
X
goto yyo&erflowG
Y
QFFyy''p E yy'tate E yytableNyynPG
QFFyy&'p E yyl&alG
goto yyloopG
Y
el'e
X
Tif <<D519.
if (yydebug#
printf(HW'debug7 error reco&ery di'carding 'tate WdZnH0
<<P%5=*]0 Qyy''p#G
B
Tendif
if (yy''p IE yy''# goto yyabortG
@@yy''pG
@@yy&'pG
Y
Y
Y
el'e
X
if (yychar EE 0# goto yyabortG
Tif <<D519.
if (yydebug#
X
yy' E 0G
if (yychar IE <<M)]+3K58# yy' E yynameNyycharPG
if (\yy'# yy' E Hillegal@'ymbolHG
printf(HW'debug7 'tate Wd0 error reco&ery di'card' to(en Wd (W'#ZnH0
<<P%5=*]0 yy'tate0 yychar0 yy'#G
Y
Tendif
yychar E <<5MP+<G
goto yyloopG
Y
yyreduce7
Tif <<D519.
if (yydebug#
printf(HW'debug7 'tate Wd0 reducing by rule Wd (W'#ZnH0
<<P%5=*]0 yy'tate0 yyn0 yyruleNyynP#G
Tendif
yym E yylenNyynPG
if (yym#
yy&al E yy&'pN1@yymPG
el'e
M
mem'et(4yy&al0 00 'i?eof yy&al#G
'witch (yyn#
X
ca'e 17
Tline , Hte't$yH
Xprintf(Hthe &alue of e>pre''ion i' WdZnH0yy&'pN0P#GY
brea(G
ca'e !7
Tline M Hte't$yH
Xyy&alEyy&'pN@!PFyy&'pN0PGY
brea(G
ca'e "7
Tline U Hte't$yH
Xyy&alEyy&'pN0PGY
brea(G
ca'e ,7
Tline 10 Hte't$yH
Xyy&alEyy&'pN@!PQyy&'pN0PGY
brea(G
ca'e B7
Tline 1! Hte't$yH
Xyy&alEyy&'pN0PGY
brea(G
ca'e M7
Tline 1, Hte't$yH
Xyy&alEyy&'pN0PGY
brea(G
Tline "", Hy$tab$cH
Y
yy''p @E yymG
yy'tate E Qyy''pG
yy&'p @E yymG
yym E yylh'NyynPG
if (yy'tate EE 0 44 yym EE 0#
S
X
Tif <<D519.
if (yydebug#
printf(HW'debug7 after reduction0 'hifting from 'tate 0 toZ
'tate WdZnH0 <<P%5=*]0 <<=*8);#G
Tendif
yy'tate E <<=*8);G
QFFyy''p E <<=*8);G
QFFyy&'p E yy&alG
if (yychar I 0#
X
if ((yychar E yyle>(## I 0# yychar E 0G
Tif <<D519.
if (yydebug#
X
yy' E 0G
if (yychar IE <<M)]+3K58# yy' E yynameNyycharPG
if (\yy'# yy' E Hillegal@'ymbolHG
printf(HW'debug7 'tate Wd0 reading Wd (W'#ZnH0
<<P%5=*]0 <<=*8);0 yychar0 yy'#G
Y
Tendif
Y
if (yychar EE 0# goto yyacceptG
goto yyloopG
Y
if ((yyn E yyginde>NyymP# 44 (yyn FE yy'tate# JE 0 44
yyn IE <<+)1;5S*O5 44 yychec(NyynP EE yy'tate#
yy'tate E yytableNyynPG
el'e
yy'tate E yydgotoNyymPG
Tif <<D519.
if (yydebug#
printf(HW'debug7 after reduction0 'hifting from 'tate Wd Z
U
to 'tate WdZnH0 <<P%5=*]0 Qyy''p0 yy'tate#G
Tendif
if (yy''p JE yy''lim 44 yygrow'tac((##
X
goto yyo&erflowG
Y
QFFyy''p E yy'tateG
QFFyy&'p E yy&alG
goto yyloopG
yyo&erflow7
yyerror(Hyacc 'tac( o&erflowH#G
yyabort7
return (1#G
yyaccept7
return (0#G
Y
How to compile:
ayacc Ifile nameJ
acc y$tab$c bll @ly
INPUT: !F"QB
OUTPUT: +he &alue of e>pre''ion i' 1S$