Write a C++ program that simulatesa lexical/syntax analyzer (parser) system for
ID: 3807151 • Letter: W
Question
Write a C++ program that simulatesa lexical/syntax analyzer (parser) system for the following simple if ..else..statement.
The BNF is:
<ifstmt>-> if (<boolexpr>) <assign> | if (<boolexpr>) <assign> else <assign>
<boolexpr>-> <boolexpr> OR <boolterm> | <boolterm>
<boolterm>-> <boolterm> AND <boolfactor> | <boolfactor>
<boolfactor>-> TRUE | FALSE | NOT <boolfactor> | (<boolexpr>)
<assign>-> id = <expr>; | id = <expr>; <assign>
<expr>-> <expr> + <term> | <expr> - <term> | <term>
<term> -> <term> * <factor> | <term> / <factor> | <term> % <factor> | <factor>
<factor>-> id | int_constant | (<expr>)
The EBNF is:
<ifstmt>-> if (<boolexpr>) <assign> [else <assign>]
<boolexpr>-> <boolterm> {OR <boolterm>}
<boolterm>-> <boolfactor> {AND <boolfactor>}
<boolfactor>-> TRUE | FALSE | NOT <boolfactor> | (<boolexpr>)
<assign>-> id = <expr>; | id = <expr>; <assign>
<expr>-> <term> {( + | -) <term>}
<term>-> <factor> {( *|/|% ) <factor>}
<factor>-> id | int_constant | (<expr>)
NOTE:
Operator/keyword: +,-, *, /, %, (, ), ;, TRUE, FALSE, OR, AND, NOT, if, else
id: (a+b+...+z+A+B+...Z)(a+b+...+z+A+B+...Z+0+1+2+...+9)*
int_constant: (0+1+2+...+9)(0+1+2+...+9)*
/********************************************************************/
Sample syntax #1:
if (TRUE AND FALSE)
A=sum / (total+ 47);
B=( 4+ A)%3;
C=B - 15;
Sample syntax #2:
if (FALSE OR (NOT TRUE))
A= sum *( total- 47);
B=(4+ A )%3;
C=B-15;
else
A=(sum + total) * 4;
B=B+1;
C=B/2;
------------------------------------------------------------------------------------------------------------------------
Lexical Analyzer - C++
/* Filename: LexicalAnalyzer.cpp
CSC 340 - Programming Languages
Dr.
A lexical analyzer system for simple arithmetic expressions.
Operator/keyword: +, -, * /, (, )
Identifer: (a+b+...+z+A+B+...Z)(a+b+...+z+A+B+...Z+0+1+2+...+9)*
Integer: (0+1+2+...+9)(0+1+2+...+9)*
*/
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
/* Global declarations */
/* Variables */
int charClass;
char lexeme[100];
char nextChar;
int lexLen;
int token;
int nextToken;
ifstream in_fp("syntax.txt");
/* Function declarations */
void getChar();
void addChar();
void getNonBlank();
int lex(); // to get the nextToken
/* Character classes */
#define LETTER 0
#define DIGIT 1
#define UNKNOWN 99
/* Token codes */
#define INT_LIT 10
#define IDENT 11
#define ADD_OP 21
#define SUB_OP 22
#define MULT_OP 23
#define DIV_OP 24
#define LEFT_PAREN 25
#define RIGHT_PAREN 26
/******************************************************/
/* main driver */
void main()
{
/* Open the input data file and process its contents */
if (in_fp.fail())
{
cout << "File could not be opened ";
cin.get();
exit(1);
}
else {
getChar();
do {
lex(); // Getting the nextToken
} while (nextToken != EOF);
}
in_fp.close();
}
/*****************************************************/
/* lookup - a function to lookup operators and parentheses
and return the token */
int lookup(char ch) {
switch (ch) {
case '(':
addChar();
nextToken = LEFT_PAREN;
break;
case ')':
addChar();
nextToken = RIGHT_PAREN;
break;
case '+':
addChar();
nextToken = ADD_OP;
break;
case '-':
addChar();
nextToken = SUB_OP;
break;
case '*':
addChar();
nextToken = MULT_OP;
break;
case '/':
addChar();
nextToken = DIV_OP;
break;
default:
addChar();
nextToken = EOF;
break;
}
return nextToken;
}
/*****************************************************/
/* addChar - a function to add nextChar to lexeme */
void addChar() {
if (lexLen <= 98) {
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = 0;
}
else
cout << " Error - lexeme is too long ";
}
/*****************************************************/
/* getChar - a function to get the next character of
input and determine its character class */
void getChar() {
in_fp.get(nextChar);
if (in_fp.eof()) // if no more character in the file
nextChar = EOF;
if (nextChar != EOF) {
if (isalpha(nextChar))
charClass = LETTER;
else if (isdigit(nextChar))
charClass = DIGIT;
else charClass = UNKNOWN;
}
else
charClass = EOF;
}
/*****************************************************/
/* getNonBlank - a function to call getChar until it
returns a non-whitespace character */
void getNonBlank() {
while (isspace(nextChar))
getChar();
}
/***************************************************** /
/* lex - a simple lexical analyzer for arithmetic
expressions */
int lex() {
lexLen = 0;
getNonBlank();
switch (charClass) {
/* Parse identifiers */
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT) {
addChar();
getChar();
}
nextToken = IDENT;
break;
/* Parse integer literals */
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT) {
addChar();
getChar();
}
nextToken = INT_LIT;
break;
/* Parentheses and operators */
case UNKNOWN:
lookup(nextChar);
getChar();
break;
/* EOF */
case EOF:
nextToken = EOF;
lexeme[0] = 'E';
lexeme[1] = 'O';
lexeme[2] = 'F';
lexeme[3] = 0;
break;
} /* End of switch */
cout << "Next token is: " << nextToken
<< " Next lexeme is " << lexeme << " ";
return nextToken;
} /* End of function lex */
Explanation / Answer
The if...else if...else Statement:----
A lexical analyzer system for simple arithmetic expressions-
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
/* Global declarations */
/* Variables */
A simple lexical analyzer for arithmetic expressions */
int lex() {
lexLen = 0;
getNonBlank();
switch (charClass) {
/* Parse identifiers */
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT) {
addChar();
getChar();
}
nextToken = IDENT;
break;
/* Parse integer literals */
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT) {
addChar();
getChar();
}
nextToken = INT_LIT;
break;
/* Parentheses and operators */
case UNKNOWN:
lookup(nextChar);
getChar();
break;
/* EOF */
case EOF:
nextToken = EOF;
lexeme[0] = 'E';
lexeme[1] = 'O';
lexeme[2] = 'F';
lexeme[3] = 0;
break;
} /* End of switch */
cout << "Next token is: " << nextToken
<< " Next lexeme is " << lexeme << " ";
return nextToken;
} /* End of function lex */
int charClass;
char lexeme[100];
char nextChar;
int lexLen;
int token;
int nextToken;
ifstream in_fp("syntax.txt");
/* Function declarations */
void getChar();
void addChar();
void getNonBlank();
int lex(); // to get the nextToken
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.