ASSIGNMENTNO.
:12
TITLE:   AssignmenttounderstandbasicsyntaxofYACCspecification,builtinfunctions andvariablesandtodevelopaprogramforcalculator. PROBLEMSTATEMENT: WriteaprogramusingYACCforsimpledeskcalculator.
THEORY:
INTRODUCTION: YACC The  UNIX  utility yacc (Yet  Another Compiler)  parses  a stream  of token,  typically generatedbylex,accordingtoauserspecifiedgrammar.
 Structureofa yacc   file: 
Ayaccfilelooksmuchlikealexfile: %{
...definitions... %} %% ...rules... %% ...subroutines...
Definitions:Allcodebetween%{and%}iscopiedtothebeginningoftheresultingC file. Rules:Anumberofcombinationsofpatternandaction:iftheactionismorethana singlecommanditneedstobeinbraces. Code:Thiscanbeveryelaborate,butthemainingredientisthecalltoyylex,the lexicalanalyzer.Ifthecodesegmentisleftout,adefaultmainisusedwhichonlycalls yylex.
FUNCTIONSANDVARIABLESUSEDINTHISPROGRAM:
  yyerror:  Intheeventofsyntaxerrorsyacccallstheusersuppliedfunctionyyerror.
yyparse():   Thisfunctioncallstheyylex()functionwhenitrequirestokensand generatesthesyntaxtree.
 yylex():  Toobtaintokensyacccallsyylex.Functionyylexhasareturntypeofint thatreturnsatoken.
 yylval:  Itisavariablewhichstoresthevalueinthetokens.Itsreturntypeisint.
Internallyyaccmaintainstwostacksinmemory;aparsestackandavaluestack. Theparsestackcontainsterminalsandnonterminalsthatrepresentthecurrent parsingstate.Thevaluestackisanarraythatassociatesavaluewitheachelementin theparsestack.ForexamplewhenlexreturnsanVALUEtokenyaccshiftsthistoken totheparsestack.Atthesametimethecorrespondingyylvalisshiftedtothevalue stack.Theparseandvaluestacksarealwayssynchronizedsofindingavaluerelated toatokenonthestackiseasilyaccomplished.
SIMPLECALCULATOR: Thiscalculatorevaluatessimplearithmeticexpressions.Thelexprogram
matchesnumbersandoperatorsandreturnsthem;itignoreswhitespace,returns newlines,andgivesanerrormessageonanythingelse.
ALGORITHMFORSIMPLECALCULATOR:
 1. Inthedefinitionsection,firstdeclarethevariablesfortokenVALUE,andalso theleftandrightassociativerulesasfollows: left'+''', left'*''/''%', rightUMINUS. '+'and''havelowpriorityorprecedenceinanexpressionascomparedto'*','/', and'%'. 2. Inrulesection,formnumberofrulesaccordingtoprogramrequirements a. for+,$1+$3 b. for,$1$3 c. for*,$1*$3
d. for/,$1/$3 e. foruminus,$2 f. forthetermsinbetweenthe'('')',$$=$2 g. Notethat$2storestheresultofunaryexpressions. 3. Initially,themain()functioncallstheprintfstatementwhichallowsuserto enteranexpressionontheconsole. 4. Thisleadstoacalltoyyparse()whichfurthercallsyylex(). 5. Withthehelpofyylex(),getthecharacterstillthespaceisencountered. 6. Nowitwillcheckforcondition,ifitisadigit,performtheoperationandstoreit inthevariableyylvalandreturnthenumberintheformofinteger.
 C ONCLUSION:  Thus,Wesuccessfullystudiedandimplementedprogramforsimple calculator.