// API callback
related_results_labels_thumbs({"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$blogger":"http://schemas.google.com/blogger/2008","xmlns$georss":"http://www.georss.org/georss","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$thr":"http://purl.org/syndication/thread/1.0","id":{"$t":"tag:blogger.com,1999:blog-913600556879440043"},"updated":{"$t":"2024-01-01T15:38:08.302+05:30"},"category":[{"term":"Java"},{"term":"General"},{"term":"C Plus Plus Programming"},{"term":"Selenium Automation Testing"},{"term":"High Performance Computing"},{"term":"PPL"},{"term":"Parallel Computing"},{"term":"My Vlogs"},{"term":"Blogging Tips"},{"term":"Skill Development Lab"},{"term":"Compiler"},{"term":"Website Designing"},{"term":"Data Structures"},{"term":"Android"},{"term":"Database"},{"term":"CUDA"},{"term":"OPENMP"},{"term":"YouTube Tips"},{"term":"Youtube"},{"term":"C Programming"},{"term":"How To Write Blog"},{"term":"Android App Development"},{"term":"Salesforce"},{"term":"Multithreading"},{"term":"Networking"},{"term":"Swing"},{"term":"LEX \u0026 YACC"},{"term":"Linux"},{"term":"Ubuntu"},{"term":"Mysql"},{"term":"Nanded City Pune"},{"term":"GATE"},{"term":"Cloud Computing"},{"term":"MS Excel Formulas \u0026 Functions"},{"term":"Applet"},{"term":"Computer Networks"},{"term":"Google Forms"},{"term":"SQL"},{"term":"MPI"},{"term":"OpenMPI"},{"term":"Socket Programming"},{"term":"Thread Pool"},{"term":"Amazon Links Summary"},{"term":"Fedora"},{"term":"Guest Blogging"},{"term":"Python"},{"term":"SDL"},{"term":"Wine"},{"term":"Abbreviations in Computer Science"},{"term":"Amazon Affiliate Program"},{"term":"Dia Software"},{"term":"Lisp"},{"term":"Mathematics"},{"term":"OPENCL"},{"term":"Udemy Courses"},{"term":"Unboxing \u0026 Review"},{"term":"Vedic Mathematics"},{"term":"Vocabulary"}],"title":{"type":"text","$t":"Computer Revolution (www.comrevo.com)"},"subtitle":{"type":"html","$t":""},"link":[{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https:\/\/www.comrevo.com\/feeds\/posts\/default"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/-\/LEX+%26+YACC?alt=json-in-script\u0026max-results=6"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.comrevo.com\/search\/label\/LEX%20%26%20YACC"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"},{"rel":"next","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/-\/LEX+%26+YACC\/-\/LEX+%26+YACC?alt=json-in-script\u0026start-index=7\u0026max-results=6"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"generator":{"version":"7.00","uri":"http://www.blogger.com","$t":"Blogger"},"openSearch$totalResults":{"$t":"8"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"6"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-2807727434049283855"},"published":{"$t":"2017-11-08T16:39:00.000+05:30"},"updated":{"$t":"2017-11-08T16:52:20.107+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Compiler"},{"scheme":"http://www.blogger.com/atom/ns#","term":"LEX \u0026 YACC"}],"title":{"type":"text","$t":"Lexical Analyzer for Sample Language"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; In this post, we will see LEX code to generate Lexical Analyzer for sample language.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Lexical Analyzer reads the input character by character.\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003EEach meaningful word in input code is called as Lexeme. Token is a class or category of Lexeme.\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;Lexical Analyzer\u0026nbsp;tokenize the input code i.e. it decides the Token for each\u0026nbsp;Lexeme based on the Regular Expressions or Patterns that we provide to the LEX program.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Lexical Analyzer skips or avoid the white spaces (e.g. tab).\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Lexical analyzer makes an entry of each variable into symbol table.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Lexemes which do not match with patterns provided in LEX file are considered as lexical errors or invalid characters.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Go through the following program. It will clear your doubts regarding the functioning of Lexical Analyzer. Here, we are providing input from terminal. (You can also provide input through text file. For this, check \u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html\" target=\"_blank\"\u003Ehttp:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html\u003C\/a\u003E).\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb style=\"font-size: large;\"\u003E\u003Cspan style=\"font-size: large;\"\u003EProgram (lexicalanalyzer.l)\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Ctable border=\"1\"\u003E\u003Ctbody\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003Cspan style=\"font-size: large;\"\u003E%{\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E#include\u0026lt;stdio.h\u0026gt;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Echar name[20];\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eint i=0;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%%\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Emain {printf(\"%s: main function.\\n\",yytext);}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[(] {printf(\"%s: opening bracket.\\n\",yytext);}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[)] {printf(\"%s: closing bracket.\\n\",yytext);}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[{] {printf(\"%s: opening Curly bracket.\\n\",yytext);}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[}] {printf(\"%s: closing Curly bracket.\\n\",yytext);}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Evoid|int|float|char {printf(\"%s: data type.\\n\",yytext);}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\" \" ;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[a-zA-Z]+ {printf(\"%s: Identifier.\\n\",yytext); strcpy(name, yytext);}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[;] {printf(\"%s: Semi Colon .\\n\",yytext);}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[\\t] ;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E. {printf(\"Lexical Error:%s is Unknown Character\\n\",yytext);}\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[\\n] return 0;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%%\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eint yywrap()\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;{\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; return 1;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eint main()\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;{\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; printf(\"Enter Input (Sample Language):\\n\");\u003Cspan class=\"Apple-tab-span\" style=\"white-space: pre;\"\u003E \u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; yylex();\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; printf(\"\\nSymbol Table\\n\");\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; printf(\"Variable\\n\");\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; printf(\"%s\\n\",name);\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; return 0;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;}\u003C\/span\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb style=\"font-family: 'times new roman'; font-size: x-large;\"\u003EHow To Run:\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cdiv dir=\"ltr\" style=\"orphans: auto; text-align: left; text-indent: 0px; widows: 1;\" trbidi=\"on\"\u003E\n\u003Cdiv style=\"font-family: 'times new roman';\"\u003E\n\u003Cdiv style=\"margin: 0px;\"\u003E\n\u003Ctable border=\"1\"\u003E\u003Ctbody\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003Cdiv style=\"margin: 0px;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eparag@parag-Inspiron-N4010:~\/Desktop\/programs$ lex lexicalanalyzer.l\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eparag@parag-Inspiron-N4010:~\/Desktop\/programs$ gcc lex.yy.c\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eparag@parag-Inspiron-N4010:~\/Desktop\/programs$ .\/a.out\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003Cdiv style=\"margin: 0px;\"\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003Cdiv style=\"margin: 0px;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"font-family: 'Times New Roman';\"\u003E\n\u003Cdiv style=\"margin: 0px;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EOutput:\u003C\/b\u003E\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Ctable border=\"1\"\u003E\u003Ctbody\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003Cdiv style=\"margin: 0px;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eparag@parag-Inspiron-N4010:~\/Desktop\/programs$ lex lexicalanalyzer.l\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eparag@parag-Inspiron-N4010:~\/Desktop\/programs$ gcc lex.yy.c\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eparag@parag-Inspiron-N4010:~\/Desktop\/programs$ .\/a.out\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003EEnter Input (Sample Language):\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eint main() {float b; $$}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eint: data type.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Emain: main function.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E(: opening bracket.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E): closing bracket.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E{: opening Curly bracket.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Efloat: data type.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eb: Identifier.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E;: Semi Colon .\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003ELexical Error:$ is Unknown Character\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003ELexical Error:$ is Unknown Character\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E}: closing Curly bracket.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003ESymbol Table\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003EVariable\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eb\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003Cdiv style=\"margin: 0px;\"\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003Cdiv style=\"margin: 0px;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003ECheck\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cspan style=\"background-color: #fefdfa; font-family: \u0026quot;georgia\u0026quot; , \u0026quot;utopia\u0026quot; , \u0026quot;palatino linotype\u0026quot; , \u0026quot;palatino\u0026quot; , serif;\"\u003E\u003Cb\u003ELEX YACC program to evaluate arithmetic expression in this link\u003C\/b\u003E\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2015\/06\/lex-and-yacc-programs-for-arithmetic-expression-evaluation.html\" target=\"_blank\"\u003Ehttp:\/\/www.comrevo.com\/2015\/06\/lex-and-yacc-programs-for-arithmetic-expression-evaluation.html\u003C\/a\u003E.\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"background-color: #fefdfa; font-family: \u0026quot;georgia\u0026quot; , \u0026quot;utopia\u0026quot; , \u0026quot;palatino linotype\u0026quot; , \u0026quot;palatino\u0026quot; , serif;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"background-color: #fefdfa; font-family: \u0026quot;georgia\u0026quot; , \u0026quot;utopia\u0026quot; , \u0026quot;palatino linotype\u0026quot; , \u0026quot;palatino\u0026quot; , serif;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003Cb\u003ECheck\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cspan style=\"background-color: #fefdfa; font-family: \u0026quot;georgia\u0026quot; , \u0026quot;utopia\u0026quot; , \u0026quot;palatino linotype\u0026quot; , \u0026quot;palatino\u0026quot; , serif;\"\u003E\u003Cb\u003ELEX YACC programs To Parse Input Text File in this link\u003C\/b\u003E\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html\" target=\"_blank\"\u003Ehttp:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html\u003C\/a\u003E.\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"font-family: 'Times New Roman';\"\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.comrevo.com\/feeds\/2807727434049283855\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.comrevo.com\/2017\/11\/lexical-analyzer-for-sample-language.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/2807727434049283855"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/2807727434049283855"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.comrevo.com\/2017\/11\/lexical-analyzer-for-sample-language.html","title":"Lexical Analyzer for Sample Language"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-4464180864329090480"},"published":{"$t":"2017-05-03T16:00:00.002+05:30"},"updated":{"$t":"2017-08-31T09:28:28.051+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Compiler"},{"scheme":"http://www.blogger.com/atom/ns#","term":"LEX \u0026 YACC"}],"title":{"type":"text","$t":"LEX YACC program to check \/ recognize valid Arithmetic Expression"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003Cb style=\"font-size: x-large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003C\/b\u003E\u003Cspan style=\"font-size: large;\"\u003EIn this post, we will see LEX and YACC programs to check or\u0026nbsp;recognize\u0026nbsp;valid arithmetic expression. This expression may\u0026nbsp;have operations like Addition(+), Subtraction(-), Multiplication(*), Division(\/) or Modulus(%). Expression may contain balanced round brackets.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp; Explanation of the program, is given at the end of this post.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003C\/b\u003EGo through the following programs:\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003ELex Program: (sample.l)\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background-color: lightgreen;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%{\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E#include\u0026lt;stdio.h\u0026gt;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E#include \"y.tab.h\"\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%%\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[a-zA-Z]+ return VARIABLE;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[0-9]+ return NUMBER;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[\\t] ;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E[\\n] return 0;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E. return yytext[0];\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%%\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Eint yywrap()\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E{\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003Ereturn 1;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EYacc program: (sample.y)\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background-color: lightgreen;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%{\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; #include\u0026lt;stdio.h\u0026gt;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%token NUMBER\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%token VARIABLE\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%left '+' '-'\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%left '*' '\/' '%'\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%left '(' ')'\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%%\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003ES: VARIABLE'='E {\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;printf(\"\\nEntered arithmetic expression is Valid\\n\\n\");\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;return 0;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp;}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003EE:E'+'E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;|E'-'E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;|E'*'E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;|E'\/'E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;|E'%'E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;|'('E')'\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;| NUMBER\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;| VARIABLE\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%%\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003Evoid main()\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E{\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp;printf(\"\\nEnter Any Arithmetic Expression which can have operations Addition, Subtraction, Multiplication, Divison, Modulus and Round brackets:\\n\");\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp;yyparse();\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E}\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003Evoid yyerror()\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E{\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp;printf(\"\\nEntered arithmetic expression is Invalid\\n\\n\");\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EHow To Run:\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"background-color: lightgreen;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026gt;\u0026gt;\u0026gt;yacc -d sample.y\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026gt;\u0026gt;\u0026gt;lex sample.l\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026gt;\u0026gt;\u0026gt;gcc lex.yy.c y.tab.c\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026gt;\u0026gt;\u0026gt;.\/a.out\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv align=\"center\"\u003E\n\u003Cdiv style=\"text-align: left;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EOutput:\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: center;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EFor Valid Expressions\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both;\"\u003E\n\u003C\/div\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"https:\/\/4.bp.blogspot.com\/-kDgeFwBDFHE\/WQmw_4VV78I\/AAAAAAAADcw\/CMLeIaGqBDgU5S27n90KI11ZWA_SncJZwCLcB\/s1600\/lexyaccvalid.jpeg\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" src=\"https:\/\/4.bp.blogspot.com\/-kDgeFwBDFHE\/WQmw_4VV78I\/AAAAAAAADcw\/CMLeIaGqBDgU5S27n90KI11ZWA_SncJZwCLcB\/s1600\/lexyaccvalid.jpeg\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: center;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EFor Invalid Expressions\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003C\/div\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: center;\"\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Ca href=\"https:\/\/1.bp.blogspot.com\/-CR2JS9rXulA\/WQmxHq2h8aI\/AAAAAAAADc0\/mV5C29kcIcU_2IkT8Amrt0_fGm8WCiUawCLcB\/s1600\/lexyaccinvalid.jpeg\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" src=\"https:\/\/1.bp.blogspot.com\/-CR2JS9rXulA\/WQmxHq2h8aI\/AAAAAAAADc0\/mV5C29kcIcU_2IkT8Amrt0_fGm8WCiUawCLcB\/s1600\/lexyaccinvalid.jpeg\" \/\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: left;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003EExplanation:\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: center;\"\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003ELEX\u003C\/span\u003E\u003C\/b\u003E\u003C\/div\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; LEX program consists of three sections\u0026nbsp;\u003Cu\u003E\"Definitions\"\u003C\/u\u003E,\u0026nbsp;\u003Cu\u003E\"Regular Expressions and action for each regular expression\"\u003C\/u\u003E\u0026nbsp;and\u0026nbsp;\u003Cu\u003E\"Subroutines\"\u003C\/u\u003E.\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003Cb\u003EDefinition\u003C\/b\u003E\u0026nbsp;section consists of C language code which involves header file inclusion, global variables declaration\/definition etc. C language code can be mentioned in between the symbols\u0026nbsp;\u003Cb\u003E%{\u0026nbsp;\u003C\/b\u003Eand\u003Cb\u003E\u0026nbsp;%}\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; LEX requires regular expressions or patterns to identify token of lexemes. Examples of token are identifier, header file, constants etc. while Lexemes are the actual words used in your input program for e.g. printf, scanf, stdio.h etc. These regular expressions and action for them are mentioned in second section. When we call\u0026nbsp;\u003Cb\u003Eyylex()\u003C\/b\u003E\u0026nbsp;function, it starts the process of pattern matching. Lex keep the matched string into the address pointed by pointer\u0026nbsp;\u003Cb\u003Eyytext\u003C\/b\u003E. Matched string's length is kept in\u0026nbsp;\u003Cb\u003Eyyleng\u003C\/b\u003E\u0026nbsp;while value of token is kept in variable\u0026nbsp;\u003Cb\u003Eyylval.\u003C\/b\u003E\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-size: large;\"\u003EIn above program NUMBER and VARIABLE are tokens.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; Third section consists of subroutines\/functions. Lex call\u0026nbsp;\u003Cb\u003Eyywrap()\u003C\/b\u003E\u0026nbsp;function after input is over. It should return 1 when work is done or should return 0 when more processing is required.\u0026nbsp;\u003Cb\u003Eyylex()\u003C\/b\u003E\u0026nbsp;function actual starts the process of pattern matching. In above program, we have not called yylex() since\u0026nbsp;\u003Cb\u003Eyyparse()\u003C\/b\u003E\u0026nbsp;in yacc program automatically calls yylex(). Thats why there in no need to call yylex() separately. If you are writing standalone Lex program, then you have to call yylex() in main() function in Lex program.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: center;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EYACC\u003C\/b\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; YACC program also consists of three sections,\u0026nbsp;\u003Cu\u003E\"Definitions\"\u003C\/u\u003E,\u0026nbsp;\u003Cu\u003E\"Context Free Grammar and action for each production\"\u003C\/u\u003E,\u0026nbsp;\u003Cu\u003E\"Subroutines\/Functions\"\u003C\/u\u003E.\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; In first section, we can mention C language code which may consist of header files inclusion, global variables\/ Constants definition\/declaration.\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-size: large;\"\u003EC language code can be mentioned in between the symbols\u0026nbsp;\u003Cb\u003E%{\u0026nbsp;\u003C\/b\u003Eand\u003Cb\u003E\u0026nbsp;%}.\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u0026nbsp;Also we can define tokens in the first section. In above program, NUMBER is the token. We can define the\u0026nbsp;\u003Cb\u003Eassociativity\u003C\/b\u003E\u0026nbsp;of the operations (i.e. left associativity or right associativity). In above yacc program, we have specified\u0026nbsp;\u003Cb\u003Eleft\u003C\/b\u003E\u0026nbsp;associativity for all operators.\u0026nbsp;\u003Cb\u003EPriorities\u003C\/b\u003E\u0026nbsp;among the operators can also be specified. It is in the increasing order\u0026nbsp;\u003Cu\u003E\"from top to bottom\"\u003C\/u\u003E. For e.g. in our above yacc program, round brackets '(',')' has the higher priority than '*', '\/', '%' which has higher priority than '+', '-'. Operators in the same statement have the same priority. For e.g. in our above program\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003Eall of the '*', '\/', '%' have the same priority.\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; In second section, we mention the grammar productions and the action for each production.\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; Third section consists of the subroutines. We have to call\u0026nbsp;\u003Cb\u003Eyyparse()\u003C\/b\u003E\u0026nbsp;to initiate the parsing process.\u0026nbsp;\u003Cb\u003Eyyerror()\u003C\/b\u003E\u0026nbsp;function is called when all productions in the grammar in second section doesn't match to the input statement.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n\u003Cdiv style=\"text-align: center;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cspan style=\"font-size: medium;\"\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/compiler.html\" target=\"_blank\"\u003E\u003Cb\u003E\u003C\/b\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Ccenter\u003E\n\u003Ctable border=\"1\"\u003E\u003Ctbody\u003E\n\u003Ctr\u003E\u003Cth\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/compiler.html\" target=\"_blank\"\u003E\u003Cspan style=\"font-size: large;\"\u003ECheck Other Posts on Compiler\u003C\/span\u003E\u003C\/a\u003E\u003C\/th\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003C\/center\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/compiler.html\" target=\"_blank\"\u003E\u003Cb\u003E\u003C\/b\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Ccenter\u003E\n\u003Ctable border=\"1\"\u003E\u003Ctbody\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/09\/lex-yacc.html\" target=\"_blank\"\u003ECheck Other Posts on Lex YACC\u003C\/a\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003C\/center\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"text-align: left;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cu\u003E\u003Ci\u003ENext:\u003C\/i\u003E\u003C\/u\u003E\u0026nbsp;\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html\"\u003ELex yacc Input File Example\u003C\/a\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2015\/08\/how-to-find-first-and-follow-of-grammar-with-examples.html\"\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: left;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: left;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cu\u003E\u003Ci\u003EPrevious:\u003C\/i\u003E\u003C\/u\u003E\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2015\/05\/six-phases-of-compiler-with-example-preprocessor-assembler-linker-loader-lexical-analysis-parser-syntax.html\"\u003ESix phases of Compiler with an Example\u0026nbsp;\u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.comrevo.com\/feeds\/4464180864329090480\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.comrevo.com\/2017\/05\/lex-yacc-program-to-check-validity-of-arithmetic-expression.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/4464180864329090480"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/4464180864329090480"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.comrevo.com\/2017\/05\/lex-yacc-program-to-check-validity-of-arithmetic-expression.html","title":"LEX YACC program to check \/ recognize valid Arithmetic Expression"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/4.bp.blogspot.com\/-kDgeFwBDFHE\/WQmw_4VV78I\/AAAAAAAADcw\/CMLeIaGqBDgU5S27n90KI11ZWA_SncJZwCLcB\/s72-c\/lexyaccvalid.jpeg","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-7536634674488243452"},"published":{"$t":"2016-09-01T19:56:00.005+05:30"},"updated":{"$t":"2017-11-08T16:44:24.256+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Compiler"},{"scheme":"http://www.blogger.com/atom/ns#","term":"LEX \u0026 YACC"}],"title":{"type":"text","$t":"LEX \u0026 YACC"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Go through the following posts:\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E1.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2015\/06\/lex-and-yacc-programs-for-arithmetic-expression-evaluation.html\" target=\"_blank\"\u003ELEX YACC program to evaluate arithmetic expression\u003C\/a\u003E\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E2.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2017\/05\/lex-yacc-program-to-check-validity-of-arithmetic-expression.html\" target=\"_blank\"\u003ELEX YACC program to check validity of Arithmetic Expression\u003C\/a\u003E\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E3.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2017\/11\/lexical-analyzer-for-sample-language.html\" target=\"_blank\"\u003ELexical Analyzer for Sample Language\u003C\/a\u003E\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E4.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html\" target=\"_blank\"\u003ELEX YACC To Parse Input Text File\u003C\/a\u003E\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E5.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/concurrent-yacc-to-parse-input-text-file.html\" target=\"_blank\"\u003EConcurrent YACC to Parse Input Text File\u003C\/a\u003E\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E6.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2015\/10\/intermediate-code-generation-for-sample-language-using-LEX-and-YACC.html\" target=\"_blank\"\u003EIntermediate code generation for sample language using LEX and YACC\u003C\/a\u003E\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr class=\"Apple-interchange-newline\" \/\u003E\n\u003Ctable border=\"1\"\u003E\u003Ctbody\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003Cspan style=\"font-family: inherit; font-size: large;\"\u003E\u003Cb\u003E\u0026nbsp;\u003C\/b\u003E\u003Cspan style=\"font-size: large; font-weight: bold;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"color: black; font-size: large;\"\u003EAsk your\u0026nbsp;\u003Cb\u003EQuestion\u003C\/b\u003E\u0026nbsp;related to\u0026nbsp;\u003Cb\u003ELEX \u0026amp; YACC\u003C\/b\u003E\u0026nbsp;as a Comment in following\u0026nbsp;\u003Cb\u003ETextbox\u003C\/b\u003E.\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: black; font-family: inherit; font-size: large;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; You can\u0026nbsp;\u003Cb\u003EAnswer\u003C\/b\u003E\u0026nbsp;the already asked Question as\u0026nbsp;\u003Cb\u003Ereply\u003C\/b\u003E\u0026nbsp;to the Question.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: black; font-family: inherit; font-size: large;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: small;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E(Note: Your\u0026nbsp;\u003Cb\u003EQuestion\/Answer\u003C\/b\u003E\u0026nbsp;will be added after validation)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.comrevo.com\/feeds\/7536634674488243452\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.comrevo.com\/2016\/09\/lex-yacc.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/7536634674488243452"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/7536634674488243452"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.comrevo.com\/2016\/09\/lex-yacc.html","title":"LEX \u0026 YACC"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-1462837843219988866"},"published":{"$t":"2016-08-31T11:55:00.005+05:30"},"updated":{"$t":"2020-05-14T13:38:18.982+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Compiler"},{"scheme":"http://www.blogger.com/atom/ns#","term":"LEX \u0026 YACC"}],"title":{"type":"text","$t":"Compiler"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; Go through the following posts:\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E1.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2017\/05\/interview-questions-on-compiler-design.html\" target=\"_blank\"\u003EInterview \/ Viva Questions on Compiler Design\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E2. \u003Ca href=\"http:\/\/www.comrevo.com\/2015\/05\/six-phases-of-compiler-with-example-preprocessor-assembler-linker-loader-lexical-analysis-parser-syntax.html\" target=\"_blank\"\u003ESix phases of Compiler with an Example \u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E3. \u003Ca href=\"http:\/\/www.comrevo.com\/2015\/06\/lex-and-yacc-programs-for-arithmetic-expression-evaluation.html\" target=\"_blank\"\u003ELEX YACC program to evaluate arithmetic expression\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003C\/span\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E4. \u003Ca href=\"http:\/\/www.comrevo.com\/2015\/08\/how-to-find-first-and-follow-of-grammar-with-examples.html\" target=\"_blank\"\u003EHow to find FIRST and FOLLOW of a grammar with examples \u003C\/a\u003E\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E5. \u003Ca href=\"http:\/\/www.comrevo.com\/2015\/09\/code-generation-using-dag-labeled-tree-implementation-in-c-programming-language.html\" target=\"_blank\"\u003ECode generation using DAG \/ labeled tree - C Language Implementation\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E6. \u003Ca href=\"http:\/\/www.comrevo.com\/2015\/09\/code-generation-using-dag-labeled-tree-cpp-code-program-implementation.html\" target=\"_blank\"\u003ECode generation using DAG \/ labeled tree - C++ Language Implementation \u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E7. \u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html\" target=\"_blank\"\u003ELEX YACC To Parse Input Text File \u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E8. \u003Ca href=\"http:\/\/www.comrevo.com\/2016\/07\/what-are-the-different-types-of-parsers.html\" target=\"_blank\"\u003EWhat are the Different Types of Parsers \u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E9. \u003Ca href=\"http:\/\/www.comrevo.com\/2016\/07\/how-to-construct-LL-1-parsing-table.html\" target=\"_blank\"\u003EHow To Construct LL(1) Parsing Table \u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E10. \u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/how-to-construct-lr-0-parsing-table.html\" target=\"_blank\"\u003EHow To Construct LR(0) Parsing Table \u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E11. \u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/how-to-construct-slr-1-parsing-table.html\" target=\"_blank\"\u003E How To Construct SLR(1) Parsing Table\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E12. \u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/how-to-construct-clr-1-parsing-table.html\" target=\"_blank\"\u003EHow To Construct CLR(1) or LR(1) Parsing Table\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cbr \/\u003E\u003C\/b\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/how-to-construct-clr-1-parsing-table.html\" target=\"_blank\"\u003E\u003C\/a\u003E13.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/how-to-construct-lalr-1-parsing-table.html\" target=\"_blank\"\u003EHow To Construct LALR(1) Parsing Table \u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E14.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/10\/recursive-descent-parser-example-in-c-language.html\" target=\"_blank\"\u003ERecursive Descent Parser Example in C language\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E15.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/concurrent-yacc-to-parse-input-text-file.html\" target=\"_blank\"\u003EConcurrent YACC to Parse Input Text File\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E16.\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2015\/10\/intermediate-code-generation-for-sample-language-using-LEX-and-YACC.html\" target=\"_blank\"\u003EIntermediate code generation for sample language using LEX and YACC\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E17.\u0026nbsp;\u003Ca href=\"https:\/\/www.comrevo.com\/2020\/02\/syntax-directed-translation-scheme-sdt.html\" target=\"_blank\"\u003ESyntax Directed Translation Scheme (SDT)\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E18. \u003Ca href=\"https:\/\/www.comrevo.com\/2020\/02\/how-c-cpp-compiler-evaluates-arithmetic-expression.html\" target=\"_blank\"\u003EHow C \/ C++ Compiler evaluates arithmetic expression \/ Side Effect of Global Variables in C \/ C++\u003C\/a\u003E\u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E19.\u0026nbsp;\u003Ca href=\"https:\/\/www.comrevo.com\/2020\/02\/side-effect-of-aliasing-with-example.html\" target=\"_blank\"\u003ESide Effect of Aliasing with Example\u0026nbsp;\u003C\/a\u003E\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E20. \u003Ca href=\"https:\/\/www.comrevo.com\/2020\/02\/how-to-create-header-file-in-c-cpp-independent-compilation-in-c-language.html\" target=\"_blank\"\u003EHow to Create Header File in C \/ CPP or Independent compilation in C language\u003C\/a\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E21. \u003Ca href=\"https:\/\/www.comrevo.com\/2020\/02\/independent-compilation-in-java-language.html\" target=\"_blank\"\u003EIndependent compilation in Java language \u003C\/a\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E22.\u0026nbsp;\u003Ca href=\"https:\/\/www.comrevo.com\/2020\/05\/code-optimization-techniques-in-compiler-design-with-examples.html\" target=\"_blank\"\u003ECode Optimization Techniques in Compiler Design with Examples\u003C\/a\u003E \u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E23. \u003Ca href=\"https:\/\/www.comrevo.com\/p\/code-generation-code-optimization.html\" target=\"_blank\"\u003ECode Generation \u0026amp; Code Optimization\u003C\/a\u003E \u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\n\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr class=\"Apple-interchange-newline\" \/\u003E\n\u003Ctable border=\"1\"\u003E\u003Ctbody\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003Cspan style=\"font-family: inherit; font-size: large;\"\u003E\u003Cb\u003E\u0026nbsp;\u003C\/b\u003E\u003Cspan style=\"font-size: large; font-weight: bold;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003C\/span\u003E\u003Cspan style=\"color: black; font-size: large;\"\u003EAsk your\u0026nbsp;\u003Cb\u003EQuestion\u003C\/b\u003E\u0026nbsp;related to \u003Cb\u003ECompiler Design\u003C\/b\u003E as a Comment in following\u0026nbsp;\u003Cb\u003ETextbox\u003C\/b\u003E.\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: black; font-family: inherit; font-size: large;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; You can\u0026nbsp;\u003Cb\u003EAnswer\u003C\/b\u003E\u0026nbsp;the already asked Question as\u0026nbsp;\u003Cb\u003Ereply\u003C\/b\u003E\u0026nbsp;to the Question.\u0026nbsp;\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"color: black; font-family: inherit; font-size: large;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003Cspan style=\"font-size: small;\"\u003E\u003Cspan style=\"font-size: large;\"\u003E(Note: Your\u0026nbsp;\u003Cb\u003EQuestion\/Answer\u003C\/b\u003E\u0026nbsp;will be added after validation)\u003C\/span\u003E\u003C\/span\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n\u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.comrevo.com\/feeds\/1462837843219988866\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.comrevo.com\/2016\/08\/compiler.html#comment-form","title":"1 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/1462837843219988866"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/1462837843219988866"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.comrevo.com\/2016\/08\/compiler.html","title":"Compiler"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"thr$total":{"$t":"1"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-7459242258943749629"},"published":{"$t":"2016-01-18T20:14:00.001+05:30"},"updated":{"$t":"2017-08-31T08:57:39.224+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Compiler"},{"scheme":"http://www.blogger.com/atom/ns#","term":"LEX \u0026 YACC"}],"title":{"type":"text","$t":"Concurrent YACC to Parse Input Text File"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Ci\u003E\u003Cspan style=\"font-size: large;\"\u003E(With the help from \u003Cb\u003EProf. Shailesh Cheke\u003C\/b\u003E)\u003C\/span\u003E\u003C\/i\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; In this post, we will see concurrent Lex-YACC program to parse a input text file which consists of several arithmetic expressions.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; To create threads, here we have used OpenMP API. To avoid race condition among threads to get access of global variables (like yylval, yyin, yytext), we have to use re-entrant parser.\u0026nbsp; \u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Go through the following program:\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EInput File: (sample.txt)\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background-color: lightgreen;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E2+3*5\u003Cbr \/\u003E5+3-4\u003Cbr \/\u003E5+5-5*5\u003Cbr \/\u003E6+4\/2-3\u003Cbr \/\u003E2+3*4\u003Cbr \/\u003E5+3-3\u003Cbr \/\u003E5+5-5*4\u003Cbr \/\u003E6+4\/2-1\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003EProgram: (sample.l)\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background-color: lightgreen;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%{\u003Cbr \/\u003E#include\u0026lt;stdio.h\u0026gt;\u003Cbr \/\u003E#include \"y.tab.h\"\u003Cbr \/\u003E\u003Cbr \/\u003E#define YYSTYPE int\u003Cbr \/\u003E%}\u003Cbr \/\u003E%option reentrant bison-bridge\u003Cbr \/\u003E\u003Cbr \/\u003E%%\u003Cbr \/\u003E[0-9]+ {\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; *yylval=atoi(yytext);\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; return NUMBER;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; }\u003Cbr \/\u003E[\\t] ;\u003Cbr \/\u003E[\\n] return 0;\u003Cbr \/\u003E. return yytext[0];\u003Cbr \/\u003E%%\u003Cbr \/\u003Eint yywrap(yyscan_t yyscanner)\u003Cbr \/\u003E{\u003Cbr \/\u003Ereturn 1;\u003Cbr \/\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003EProgram: (sample.y)\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background-color: lightgreen;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%{\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; #include\u0026lt;stdio.h\u0026gt;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; #include\u0026lt;stdlib.h\u0026gt;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; #include\u0026lt;omp.h\u0026gt;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; #define YYSTYPE int\u003Cbr \/\u003E\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; int numofthreads,expsperthread;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; char inputfilename[30];\u003Cbr \/\u003E%}\u003Cbr \/\u003E%token NUMBER\u003Cbr \/\u003E\u003Cbr \/\u003E%left '+' '-'\u003Cbr \/\u003E%left '*' '\/' '%'\u003Cbr \/\u003E%left '(' ')'\u003Cbr \/\u003E\u003Cbr \/\u003E%pure-parser\u003Cbr \/\u003E%lex-param {void * scanner}\u003Cbr \/\u003E%parse-param {void * scanner}\u003Cbr \/\u003E\u003Cbr \/\u003E%%\u003Cbr \/\u003E\u003Cbr \/\u003EArithmeticExpression: E{\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \/\/printf(\"\\nResult=%d\\n\",$$);\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; return $$;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; }\u003Cbr \/\u003EE:E'+'E {$$=$1+$3;}\u003Cbr \/\u003E\u0026nbsp;|E'-'E {$$=$1-$3;}\u003Cbr \/\u003E\u0026nbsp;|E'*'E {$$=$1*$3;}\u003Cbr \/\u003E\u0026nbsp;|E'\/'E {$$=$1\/$3;}\u003Cbr \/\u003E\u0026nbsp;|E'%'E {$$=$1%$3;}\u003Cbr \/\u003E\u0026nbsp;|'('E')' {$$=$2;}\u003Cbr \/\u003E\u0026nbsp;| NUMBER {$$=$1;}\u003Cbr \/\u003E;\u003Cbr \/\u003E\u003Cbr \/\u003E%%\u003Cbr \/\u003E\u003Cbr \/\u003Evoid func()\u003Cbr \/\u003E{\u003Cbr \/\u003Evoid * scanner;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; yylex_init(\u0026amp;scanner);\u003Cbr \/\u003E\u003Cbr \/\u003EFILE *fp;\u003Cbr \/\u003Efp=fopen(inputfilename,\"r\");\u003Cbr \/\u003E\u003Cbr \/\u003Eint i,j;\u003Cbr \/\u003E\u003Cbr \/\u003Eint tid;\u003Cbr \/\u003Eint res;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp; \u003Cbr \/\u003Etid=omp_get_thread_num();\u0026nbsp;\u0026nbsp; \u003Cbr \/\u003E\u003Cbr \/\u003Echar *s=malloc(100);\u003Cbr \/\u003Esize_t n=100;\u003Cbr \/\u003E\u003Cbr \/\u003Efor(i=0;i\u0026lt;(expsperthread*tid);i++)\u003Cbr \/\u003E\u0026nbsp; {\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; getline(\u0026amp;s,\u0026amp;n,fp);\u003Cbr \/\u003E\u0026nbsp; }\u003Cbr \/\u003E\u003Cbr \/\u003Eyyset_in(fp,scanner);\u003Cbr \/\u003E\u003Cbr \/\u003Efor(j=0;j\u0026lt;expsperthread;j++)\u003Cbr \/\u003E\u0026nbsp; {\u0026nbsp; \u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; res=yyparse(scanner); \u003Cbr \/\u003E\u0026nbsp; \u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; printf(\"%dth Expression=%d is given by Thread %d\\n\",(expsperthread*tid+j+1),res,tid); \u003Cbr \/\u003E\u0026nbsp; }\u003Cbr \/\u003E\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; yylex_destroy(scanner);\u003Cbr \/\u003E\u0026nbsp; \u003Cbr \/\u003Efclose(fp);\u003Cbr \/\u003E}\u003Cbr \/\u003E\u003Cbr \/\u003Evoid main()\u003Cbr \/\u003E{\u003Cbr \/\u003Eprintf(\"Enter Input Text File Name\\n\");\u003Cbr \/\u003Escanf(\"%s\",inputfilename);\u003Cbr \/\u003Eprintf(\"Enter Number of threads to be created\\n\");\u003Cbr \/\u003Escanf(\"%d\",\u0026amp;numofthreads);\u003Cbr \/\u003Eprintf(\"Enter Number of Arithmetic Expressions to be evaluated by each thread\\n\");\u003Cbr \/\u003Escanf(\"%d\",\u0026amp;expsperthread);\u003Cbr \/\u003E\u003Cbr \/\u003E#pragma omp parallel num_threads(numofthreads)\u003Cbr \/\u003E\u0026nbsp; {\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; func();\u003Cbr \/\u003E\u0026nbsp; }\u003Cbr \/\u003E}\u003Cbr \/\u003E\u003Cbr \/\u003Eint yyerror()\u003Cbr \/\u003E{\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp; printf(\"\\nArithmetic expression is Invalid\\n\\n\");\u003Cbr \/\u003E}\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\n\u003Cb\u003E\u003Cspan style=\"font-size: large;\"\u003EHow To Run:\u0026nbsp;\u003C\/span\u003E\u003C\/b\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026gt;\u0026gt;\u0026gt;yacc -d sample.y\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026gt;\u0026gt;\u0026gt;lex sample.l\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026gt;\u0026gt;\u0026gt;gcc -fopenmp lex.yy.c y.tab.c\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026gt;\u0026gt;\u0026gt;.\/a.out \u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EOutput:\u003C\/b\u003E\u0026nbsp;\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003C\/div\u003E\n\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Ca href=\"http:\/\/4.bp.blogspot.com\/-ucoXTNrstfc\/Vp0TcHO6M9I\/AAAAAAAACws\/poHWCjErq9I\/s1600\/lexyaccconcurrent.jpeg\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" src=\"https:\/\/4.bp.blogspot.com\/-ucoXTNrstfc\/Vp0TcHO6M9I\/AAAAAAAACws\/poHWCjErq9I\/s1600\/lexyaccconcurrent.jpeg\" \/\u003E\u003C\/a\u003E\u003C\/div\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: center;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/compiler.html\" target=\"_blank\"\u003E\u003Cb\u003E\u003C\/b\u003E\u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Ccenter\u003E\n\u003Ctable border=\"1\"\u003E \u003Ctbody\u003E\n\u003Ctr\u003E\u003Cth\u003ECheck Other Posts on Compiler\u003C\/th\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003C\/center\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/compiler.html\" target=\"_blank\"\u003E\u003Cb\u003E\n\u003C\/b\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cdiv\u003E\n\u003Ccenter\u003E\n\u003Ctable border=\"1\"\u003E\n\u003Ctbody\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/09\/lex-yacc.html\" target=\"_blank\"\u003ECheck Other Posts on Lex YACC\u003C\/a\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003C\/center\u003E\n\u003C\/div\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cu\u003E\u003Ci\u003ENext Post:\u003C\/i\u003E\u003C\/u\u003E\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2015\/08\/how-to-find-first-and-follow-of-grammar-with-examples.html\"\u003EHow to find FIRST and FOLLOW of a grammar with examples \u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cu\u003E\u003Ci\u003EPrevious Post:\u003C\/i\u003E\u003C\/u\u003E\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html\"\u003ELex Yacc Input File Example \u003C\/a\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/openmp-array-sum-parallel.html\"\u003E \u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.comrevo.com\/feeds\/7459242258943749629\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.comrevo.com\/2016\/01\/concurrent-yacc-to-parse-input-text-file.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/7459242258943749629"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/7459242258943749629"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.comrevo.com\/2016\/01\/concurrent-yacc-to-parse-input-text-file.html","title":"Concurrent YACC to Parse Input Text File"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/4.bp.blogspot.com\/-ucoXTNrstfc\/Vp0TcHO6M9I\/AAAAAAAACws\/poHWCjErq9I\/s72-c\/lexyaccconcurrent.jpeg","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-4734419884949238436"},"published":{"$t":"2016-01-09T19:22:00.004+05:30"},"updated":{"$t":"2016-09-28T11:58:40.676+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Compiler"},{"scheme":"http://www.blogger.com/atom/ns#","term":"LEX \u0026 YACC"}],"title":{"type":"text","$t":"LEX YACC To Parse Input Text File"},"content":{"type":"html","$t":"\u003Cdiv dir=\"ltr\" style=\"text-align: left;\" trbidi=\"on\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; In this post, we will see how to provide text file as input to Lex Yacc program. Here we will take example of arithmetic expression evaluation which takes arithmetic expressions from a text file.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; yyin is standard file pointer which is defined by lex yacc library. So that, just we have to declare it in yacc file by using keyword extern.\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Go through the following program:\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Ca name='more'\u003E\u003C\/a\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EProgram: (sample.txt)\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cbr \/\u003E\n\u003Cdiv style=\"background-color: lightgreen;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E2+3*5\u003Cbr \/\u003E5+3-4\u003Cbr \/\u003E5+5-5*5\u003Cbr \/\u003E6+4\/2-3\u003C\/span\u003E\u003C\/div\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EProgram: (sample.l)\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"background-color: lightgreen;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E%{\u003Cbr \/\u003E#include\u0026lt;stdio.h\u0026gt;\u003Cbr \/\u003E#include \"y.tab.h\"\u003Cbr \/\u003Eextern int yylval;\u003Cbr \/\u003E%}\u003Cbr \/\u003E\u003Cbr \/\u003E%%\u003Cbr \/\u003E[0-9]+ {\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; yylval=atoi(yytext);\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; return NUMBER;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; }\u003Cbr \/\u003E[\\t] ;\u003Cbr \/\u003E[\\n] return 0;\u003Cbr \/\u003E. return yytext[0];\u003Cbr \/\u003E%%\u003Cbr \/\u003Eint yywrap()\u003Cbr \/\u003E{\u003Cbr \/\u003Ereturn 1;\u003Cbr \/\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EProgram:(sample.y)\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv style=\"background-color: lightgreen;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u0026nbsp;%{\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; #include\u0026lt;stdio.h\u0026gt;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; int flag=0;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp; extern FILE *yyin;\u003Cbr \/\u003E\u0026nbsp; \u003Cbr \/\u003E%}\u003Cbr \/\u003E%token NUMBER\u003Cbr \/\u003E\u003Cbr \/\u003E%left '+' '-'\u003Cbr \/\u003E%left '*' '\/' '%'\u003Cbr \/\u003E%left '(' ')'\u003Cbr \/\u003E%%\u003Cbr \/\u003EArithmeticExpression: E{\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; printf(\"\\nResult=%d\\n\",$$);\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; return 0;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; }\u003Cbr \/\u003EE:E'+'E {$$=$1+$3;}\u003Cbr \/\u003E\u0026nbsp;|E'-'E {$$=$1-$3;}\u003Cbr \/\u003E\u0026nbsp;|E'*'E {$$=$1*$3;}\u003Cbr \/\u003E\u0026nbsp;|E'\/'E {$$=$1\/$3;}\u003Cbr \/\u003E\u0026nbsp;|E'%'E {$$=$1%$3;}\u003Cbr \/\u003E\u0026nbsp;|'('E')' {$$=$2;}\u003Cbr \/\u003E\u0026nbsp;| NUMBER {$$=$1;}\u003Cbr \/\u003E;\u003Cbr \/\u003E%%\u003Cbr \/\u003E\u003Cbr \/\u003Evoid main()\u003Cbr \/\u003E{\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp; FILE *fp; int i;\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp; fp=fopen(\"sample.txt\",\"r\");\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp; yyin=fp;\u003Cbr \/\u003E\u0026nbsp; \u003Cbr \/\u003E\u0026nbsp;for(i=0;i\u0026lt;4;i++)\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp; yyparse();\u003Cbr \/\u003E\u003Cbr \/\u003E\u0026nbsp; if(flag==0)\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp; printf(\"\\nEntered arithmetic expression is Valid\\n\\n\");\u003Cbr \/\u003E\u003Cbr \/\u003E}\u003Cbr \/\u003Evoid yyerror()\u003Cbr \/\u003E{\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp; printf(\"\\nEntered arithmetic expression is Invalid\\n\\n\");\u003Cbr \/\u003E\u0026nbsp;\u0026nbsp; flag=1;\u003Cbr \/\u003E}\u003C\/span\u003E\u003C\/div\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003EOutput:\u0026nbsp;\u003C\/b\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cdiv class=\"separator\" style=\"clear: both; text-align: center;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Ca href=\"http:\/\/1.bp.blogspot.com\/-nSmReez76P4\/VpETCPGPZHI\/AAAAAAAACu0\/9v-w9TdqKIQ\/s1600\/lexyaccyyin.jpeg\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"\u003E\u003Cimg border=\"0\" src=\"https:\/\/1.bp.blogspot.com\/-nSmReez76P4\/VpETCPGPZHI\/AAAAAAAACu0\/9v-w9TdqKIQ\/s1600\/lexyaccyyin.jpeg\" \/\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv\u003E\n\u003Cdiv\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cbr \/\u003E\u003C\/span\u003E\u003C\/div\u003E\n\u003Cdiv style=\"text-align: center;\"\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/08\/compiler.html\" target=\"_blank\"\u003E\u003Cb\u003E\u003Ccenter\u003E\u003Ctable border=\"1\"\u003E \u003Ctbody\u003E\n\u003Ctr\u003E\u003Cth\u003ECheck Other Posts on Compiler\u003C\/th\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\u003C\/center\u003E\n\u003C\/b\u003E\u003C\/a\u003E\u003C\/span\u003E\u003C\/div\u003E\n\n\u003Cbr\u003E\n\u003Cbr\u003E\n\u003Cdiv\u003E\n\u003Ccenter\u003E\n\u003Ctable border=\"1\"\u003E\n\u003Ctbody\u003E\n\u003Ctr\u003E\u003Ctd\u003E\u003Cspan style=\"font-size: large;\"\u003E\u003Cb\u003E\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/09\/lex-yacc.html\" target=\"_blank\"\u003ECheck Other Posts on Lex YACC\u003C\/a\u003E\u003C\/b\u003E\u003C\/span\u003E\u003C\/td\u003E\u003C\/tr\u003E\n\u003C\/tbody\u003E\u003C\/table\u003E\n\u003C\/center\u003E\n\u003C\/div\u003E\n\u003Cbr\u003E\n\u003Cbr\u003E\n\n\n\u003Cbr \/\u003E\n\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cu\u003E\u003Ci\u003ENext:\u003C\/i\u003E\u003C\/u\u003E\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2016\/01\/concurrent-yacc-to-parse-input-text-file.html\"\u003EConcurrent YACC to Parse Input Text File \u003C\/a\u003E\u003C\/span\u003E\u003Cbr \/\u003E\n\u003Cbr \/\u003E\n\u003Cspan style=\"font-size: large;\"\u003E\u003Cu\u003E\u003Ci\u003EPrevious:\u003C\/i\u003E\u003C\/u\u003E\u0026nbsp;\u003Ca href=\"http:\/\/www.comrevo.com\/2015\/06\/lex-and-yacc-programs-for-arithmetic-expression-evaluation.html\"\u003ELEX YACC program to evaluate arithmetic expression ( LEX and YACC programs for arithmetic expression evaluation ) \u003C\/a\u003E \u003C\/span\u003E\u003C\/div\u003E\n\u003C\/div\u003E\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https:\/\/www.comrevo.com\/feeds\/4734419884949238436\/comments\/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/4734419884949238436"},{"rel":"self","type":"application/atom+xml","href":"https:\/\/www.blogger.com\/feeds\/913600556879440043\/posts\/default\/4734419884949238436"},{"rel":"alternate","type":"text/html","href":"https:\/\/www.comrevo.com\/2016\/01\/lex-yacc-input-file-example.html","title":"LEX YACC To Parse Input Text File"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"http:\/\/www.blogger.com\/profile\/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"16","height":"16","src":"https:\/\/img1.blogblog.com\/img\/b16-rounded.gif"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https:\/\/1.bp.blogspot.com\/-nSmReez76P4\/VpETCPGPZHI\/AAAAAAAACu0\/9v-w9TdqKIQ\/s72-c\/lexyaccyyin.jpeg","height":"72","width":"72"},"thr$total":{"$t":"0"}}]}});