Skip to main content

Generate YACC specification for a few synthetic categories (a)Program to recognize a valid arithmetic expression that uses +,-,* and /. (b) program to recognize a valid variable which starts with a letter followed by any number of letters or digits

 Generate YACC specification for a few synthetic categories

(a)Program to recognize a valid arithmetic expression that uses +,-,* and /. (b) program to recognize a valid variable which starts with a letter followed by any number of letters or digits
a)Program to recognize a valid arithmetic expression that uses +,-,* and /.



Program-

%{ /* This LEX program returns the tokens for the expression */ #include “y.tab.h” %} %% “=” {printf(“\n Operator is EQUAL”);} “+” {printf(“\n Operator is PLUS”);} “-“ {printf(“\n Operator is MINUS”);} “/” {printf(“\n Operator is DIVISION”);} “*” {printf(“\n Operator is MULTIPLICATION”);} [a-z A-Z]*[0-9]* { printf(“\n Identifier is %s”,yytext); return ID; } return yytext[0]; \n return 0; %% int yywrap() { return 1; } Program Name : arith_id.y %{ #include /* This YYAC program is for recognizing the Expression */ %} %% statement: A’=’E | E { printf(“\n Valid arithmetic expression”); $$ = $1; }; E: E’+’ID | E’-’ID | E’*’ID | E’/’ID | ID ; %% extern FILE *yyin; main() { do { yyparse(); }while(!feof(yyin)); } yyerror(char*s) { } Output: [root@localhost]# lex arith_id.1 [root@localhost]# yacc –d arith_id.y [root@localhost]# gcc lex.yy.c y.tab.c [root@localhost]# ./a.out x=a+b; Identifier is x Operator is EQUAL Identifier is a Operator is PLUS Identifier is b

(b) program to recognize a valid variable which starts with a letter followed by any number of
letters or digits

%{
/* This LEX program returns the tokens for the Expression */
#include "y.tab.h"
%}
%%
"int " {return INT;}
"float" {return FLOAT;}
"double" {return DOUBLE;}
[a-zA-Z]*[0-9]*{
printf("\nIdentifier is %s",yytext);
return ID;
}
return yytext[0];
\n return 0;
int yywrap()
{
return 1;
}



Program name: variable_test.y

%{
#include
/* This YACC program is for recognising the Expression*/
%}
%token ID INT FLOAT DOUBLE
%%
D;T L
;
L:L,ID
|ID
;
T:INT
|FLOAT
|DOUBLE
;
%%
extern FILE *yyin;
main()
{
do
{
yyparse();
}while(!feof(yyin));
}
yyerror(char*s)
{
}

Output:

[root@localhost]# lex variable_test.I
[root@localhost]# yacc –d variable_test.y
[root@localhost]# gcc lex.yy.c y.tab.c
[root@localhost]# ./a.out
int a,b;

Identifier is a
Identifier is b[root@localhost]#

Comments

Popular posts from this blog

Create a socket for HTTP for web page upload and download

Create a socket for HTTP for web page upload and download. Aim: To write a java program for socket for HTTP for web page upload and download . Algorithm 1.Start the program. 2.Get the frame size from the user 3.To create the frame based on the user request. 4.To send frames to server from the client side. 5.If your frames reach the server it will send ACK signal to client otherwise it will send NACK signal to client. 6.Stop the program Program : Client import javax.swing.*; import java.net.*; import java.awt.image.*; import javax.imageio.*; import java.io.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Client{ public static void main(String args[]) throws Exception{ Socket soc; BufferedImage img = null; soc=new Socket("localhost",4000); System.out.println("Client is running. ");  try { System.out.println("Reading image from disk. "); im...

Write an HTML program to design an entry form of student details and send it to store at database server like SQL, Oracle or MS Access

Write an HTML program to design an entry form of student details and send it to store at database server like SQL, Oracle or MS Access <!DOCTYPE html> <html> <head> <title>PHP insertion</title> <link href="css/insert.css" rel="stylesheet"> </head> <body> <div class="maindiv"> <!--HTML Form --> <div class="form_div"> <div class="title"> <h2>Insert Data In Database Using PHP.</h2> </div> <form action="insert.php" method="post"> <!-- Method can be set as POST for hiding values in URL--> <h2>Form</h2> <label>Name:</label> <input class="input" name="name" type="text" value=""> <label>Email:</label> <input class="input" name="email" type="text" value=""> <label>Contact:</label> <inp...

Write a code simulating PING and TRACEROUTE commands

  Write a code simulating PING command     Aim:   To Write the java program for simulating ping command   Algorithm Step 1: start the program. Step 2: Include necessary package in java. Step 3: To create a process object p to implement the ping command. Step 4: declare one BufferedReader stream class object. Step 5: Get thedetails of the server          5.1: length of the IP address.          5.2: time required to get the details.          5.3: send packets , receive packets and lost packets.          5.4: minimum ,maximum and average times. Step 6: print the results. Step 7:Stop the program.   Program:   import java.io.*; import java.net.*; class pingserver { public static void main(String args[]) { try { String str; System.out.print(" Enter...