JNTU Programming Lexical Analyzer for The Calculator Grammar Project

Assignment:
The purpose of this assignment is to reinforce grammar concepts and to start building
towards the project. Specifically the assignment is to construct a lexical analyzer for
the calculator grammar discussed in the recorded lecture. The test program should
consist of a loop that will continually get the next token and display the token type and
the lexeme for that token. You need to create a test (file or string) that contains at least
one instance of each token. The LexicalAnalyzer class needs to have a method
getToken that returns a token. Note that you may want to have additional methods. You
may use my Token class and my Grammar interface or you can create your own. The
files that I have supplied are available under the files link.
Please submit your source code finals and the test data.
Calculator
Purpose – numerical calculations

Enter 1 numerical expression at a time or enter a
sequence of numerical expression (programmable
calculator

Numerical expression

Integers or real numbers

Addition, subtraction, multiplication, division,
exponentiation

Grouping symbols

Infix notation

End expression with “enter key”

1
Calculator Syntax

Calculator program – sequence of 1 or more arithmetic
expressions terminated with end of line markers


< Program > ::= < Program > < Statement >
| < Statement >



< Statement > ::= < Expression > “\n”
Using statement anticipating more types of statements
in the future
2
Calculator Syntax

Arithmetic expression – arithmetic operations on
numbers








< Expression > ::= < Expression > “+” < Expression >
| < Expression > “-” < Expression >
| < Expression > “*” < Expression >
| < Expression > “/” < Expression >
| < Expression > “^” < Expression >
| “(” < Expression > “)”
| “-” < Expression >
| < Number >
3
Calculator Syntax

Number – integer or real number


< Number >
::= < Int-Lit >
| < Float-Lit >
4
Types of Tokens
new_line – “\n”

plus – “+”

minus – “-”

times – “*”

divide – “/”

exponent – “^”

lparen – “(“

rparen – “)”

integer literal – sequence of 1 more digits

float literal – integer literal followed by “.” followed by
integer literal

5
Tokens

Token needs to hold all information needed by parser

Type of token

Lexeme (string)

Row number

Column number
6
stm New State Machine Diagram
plus
minus
times
+

*
divide
/
exponent
^
Start
\n
newline
(
)
digit
lparen
rparen
end of input
int_lit
eos
digit
digit
.
.
digit
float_lit
Token.java
package first_ver;
import first_ver.Grammar.TokenType;
public class Token
{
private TokenType tokType;
private String lexeme;
private int lineNum;
private int colNum;
public Token(TokenType tokType, String lexeme, int lineNum, int colNum) throws
LexicalError
{
this.tokType = tokType;
this.lexeme = lexeme;
if (lineNum < 0) throw new LexicalError ("invalid line number"); this.lineNum = lineNum; if (colNum < 0) throw new LexicalError ("invalid column number"); this.colNum = colNum; } /** * @return the lineNum */ public int getLineNum() { return lineNum; } /** * @return the colNum */ public int getColNum() { return colNum; } public TokenType getTokType() { return tokType; } public String getLexeme() { return lexeme; } } Grammar.java package first_ver; public interface Grammar { enum TokenType { PLUS, MINUS, TIMES, DIVIDE, EXPONENT, LPAREN, RPAREN, INTLIT, FLOATLIT, EOLN, EOS }; }

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER