You are on page 1of 6

Name – Yashmit Mavi

Roll No – BTECH/10121/18
Class- CSE ‘A’
End Semester Assignment
Q(1)

Code (Lex file):

%{

#include "calc.tab.h"

#include <stdlib.h>

void yyerror(char *);

extern YYSTYPE yylval;

char sym[26][26];

int count=0;

%}

%%

[ \t] ;

[a-zA-Z][a-zA-Z0-9_]* {

strcpy(sym[count],yytext);

yylval.string=sym[count];

count++;

if (count==26) {count=0;}

return(IDENT);
}

[0-9]*\.[0-9]+ {yylval.flt = atof(yytext); return(REAL);}

[0-9]+ {yylval.var = atoi(yytext); return(INT);}

"+" {return(ADDOP);}

"-" {return(SUBOP);}

"*" {return(MULOP);}

"/" {return(DIVOP);}

"%" {return(MODOP);}

= {return(ASSIGNOP);}

"(" {return(yytext[0]);}

")" {return(yytext[0]);}

\n {return(yytext[0]);}

. {yyerror("invalid character");}

%%

int yywrap() {

return 1;

Code (Yacc file):

%{

#include <stdio.h>

#include <math.h>

double atof();

extern char *yytext;


extern char sym[26][26];

float val[26];

int i;

%}

%union {

int var;

float flt;

char *string;

%token <var> IDENT

%token <flt> REAL

%token <var> INT

%type <flt> expr

%token ASSIGNOP

%left ADDOP SUBOP

%left MULOP DIVOP

%left MODOP

%%

prog : prog stmt '\n'

stmt : expr {printf("%f \n", $1);}

| IDENT ASSIGNOP expr {


i=0;

while (strcmp(sym[i],$1)!=0) {

i++;

if (i==26) break;

if (i<26) val[i]=$3;

expr : IDENT {

$$ = 0;

i=0;

while (strcmp(sym[i],$1)!=0) {

i++;

if (i==26) break;

if (i<26) $$=val[i];

| INT {$$ = $1;}

| REAL {$$ = $1;}

| expr ADDOP expr {$$ = $1 + $3;}

| expr SUBOP expr {$$ = $1 - $3;}

| SUBOP expr {$$ = -$2;}

| expr MULOP expr {$$ = $1 * $3;}

| expr DIVOP expr {$$ = $1 / $3;}


| expr MODOP expr {$$ = fmodf($1,$3);}

| '(' expr ')' {$$ = $2;}

%%

main() {

return(yyparse());

yyerror(char *s) {

fprintf(stderr, "%s\n", s);

Output:
Q(2)

yyin() - the input stream pointer (i.e it points to an input file which is to
be scanned or tokenised), however the default input of default main() is
stdin .

yylex() - Implies the main entry point for lex, reads the input stream
generates tokens, returns zero at the end of input stream . It is called to
invoke the lexer (or scanner) and each time yylex() is called, the scanner
continues processing the input from where it last left off.

yytext() - A buffer that holds the input characters that actually match the
pattern (i.e., lexeme) or say a pointer to the matched string.

yyerror() - is a lex & yacc library function that simply displays a text string
argument to stderr using fprintf, and returns the integer value received
from fprintf.

yymore() - The text matching a given pattern is stored in the yytext array.
In general, after the action associated with it is performed, the
characters in yytext are overwritten with succeeding characters in the
input stream to form the next match. The function yymore() prevents this
overwriting, and causes the characters matching the next pattern to be
appended to those already in yytext.

-----------------------------------------------------------------------------------------------

You might also like