Using the below EBNF Grammar create a syntax analyzer: The jflex, token, tokencl
ID: 670551 • Letter: U
Question
Using the below EBNF Grammar create a syntax analyzer:
The jflex, token, tokenclass, errormessage files. You don't need to anything to them.
But the files that need to be edited are SyntaxAnalyzer.java, and CliteSyn.java. These two files are ones you need to implement the parsing.
Also in the bottom there are test files.
Compilation Process:
Lexical Analyzer Part: Completed
Syntax Analyzer Compilation- Help needed
Source files:
%%
%{
private void echo () { System . out . print (yytext ()); }
public int position () { return yycolumn; }
%}
%class CLexer
%function nextToken
%type Token
%unicode
%line
%column
%eofval{
{ return new Token (TokenClass . EOF); }
%eofval}
Literal = [:digit:] [:digit:]*
Identifier = [:letter:] ("_"? ([:letter:] | [:digit:]))*
Variable = [:$:] ("_"? ([:letter:] | [:digit:]))*
%%
[ ] { echo (); }
"//".* { echo (); }
"+" { echo(); return new Token (TokenClass.PLUS);}
"-" { echo(); return new Token (TokenClass.MINUS);}
"*" { echo(); return new Token (TokenClass.TIMES);}
"/" { echo(); return new Token (TokenClass.SLASH);}
"(" { echo(); return new Token (TokenClass.LPAREN);}
")" { echo(); return new Token (TokenClass.RPAREN);}
"[" { echo(); return new Token (TokenClass.LBRACK);}
"]" { echo(); return new Token (TokenClass.RBRACK);}
"{" { echo(); return new Token (TokenClass.LBRACE);}
"}" { echo(); return new Token (TokenClass.RBRACE);}
"!" { echo(); return new Token (TokenClass.NOT);}
"&&" { echo(); return new Token (TokenClass.AND);}
"||" { echo(); return new Token (TokenClass.OR);}
"==" { echo(); return new Token (TokenClass.EQU);}
"=" { echo(); return new Token (TokenClass.EQ);}
"<" { echo(); return new Token (TokenClass.LT);}
">" { echo(); return new Token (TokenClass.GT);}
"!=" { echo(); return new Token (TokenClass.NE);}
"<=" { echo(); return new Token (TokenClass.LE);}
">=" { echo(); return new Token (TokenClass.GE);}
"," { echo(); return new Token (TokenClass.COMMA);}
"." { echo(); return new Token (TokenClass.PERIOD);}
":" { echo(); return new Token (TokenClass.COLON);}
";" { echo(); return new Token (TokenClass.SEMICOLON);}
"<?php" { echo(); return new Token (TokenClass.PHP);}
"?>" { echo(); return new Token (TokenClass.PHPCLOSE);}
function { echo(); return new Token (TokenClass.FUNCTION);}
return { echo(); return new Token (TokenClass.RETURN);}
if { echo(); return new Token (TokenClass.IF);}
else { echo(); return new Token (TokenClass.ELSE);}
while { echo(); return new Token (TokenClass.WHILE);}
foreach { echo(); return new Token (TokenClass.FOREACH);}
as { echo(); return new Token (TokenClass.AS);}
array { echo(); return new Token (TokenClass.ARRAY);}
array_push { echo(); return new Token (TokenClass.ARRAY_PUSH);}
print { echo(); return new Token (TokenClass.PRINT);}
array_pop { echo(); return new Token (TokenClass.ARRAY_POP);}
NULL { echo (); return new Token (TokenClass . NULL); }
{Literal} { echo (); return new Token (TokenClass . LITERAL, yytext ()); }
{Identifier} { echo (); return new Token (TokenClass . ID, yytext ()); }
{Variable} { echo (); return new Token (TokenClass . DOLLAR, yytext ()); }
//. { echo (); ErrorMessage . print (yychar, "Illegal character"); }
Token.java
// Token class definition
// Token is a class to represent lexical tokens in the Clite programming
// language, described in Programming Languages: Principles and Paradigms,
// 2nd ed., by Allen B. Tucker and Robert E. Noonan, McGraw Hill, 2007.
public class Token {
private TokenClass symbol; // current token
private String lexeme; // lexeme
public Token () { }
public Token (TokenClass symbol) {
this (symbol, null);
}
public Token (TokenClass symbol, String lexeme) {
this . symbol = symbol;
this . lexeme = lexeme;
}
public TokenClass symbol () { return symbol; }
public String lexeme () { return lexeme; }
public String toString () {
switch (symbol) {
case PHP : return "(keyword, <?php) ";
case PHPCLOSE : return "(keyword, ?>) ";
case FUNCTION : return "(keyword, function) ";
case RETURN : return "(keyword, return) ";
case IF : return "(keyword, if) ";
case ELSE : return "(keyword, else) ";
case WHILE : return "(keyword, while) ";
case FOREACH : return "(keyword, foreach) ";
case AS : return "(keyword, as) ";
case ARRAY : return "(keyword, array) ";
case ARRAY_PUSH : return "(keyword, array_push) ";
case PRINT : return "(keyword, print) ";
case ARRAY_POP : return "(keyword, array_pop) ";
case NULL : return "(keyword, NULL) ";
case COMMA : return "(punctuation, ,) ";
case SEMICOLON : return "(punctuation, ;) ";
case LBRACE : return "(operator, {) ";
case RBRACE : return "(operator, }) ";
case LPAREN : return "(operator, () ";
case RPAREN : return "(operator, )) ";
case LBRACK : return "(operator, [) ";
case RBRACK : return "(operator, ]) ";
case OR : return "(operator, ||) ";
case AND : return "(operator, &&) ";
case PLUS : return "(operator, +) ";
case MINUS : return "(operator, -) ";
case TIMES : return "(operator, *) ";
case SLASH : return "(operator, /) ";
case EQ : return "(operator, =) ";
case EQU : return "(operator, ==) ";
case NE : return "(operator, !=) ";
case LT : return "(operator, <) ";
case LE : return "(operator, <=) ";
case GT : return "(operator, >) ";
case GE : return "(operator, >=) ";
case NOT : return "(operator, !) ";
case LITERAL : return "(integer, " + lexeme + ") ";
case ID : return "(identifier, " + lexeme + ") ";
case INTEGER : return "(integer, " + lexeme + ") ";
case DOLLAR : return "(variable, " + lexeme + ") ";
default :
ErrorMessage . print (0, "Unrecognized token");
return null;
}
}
}
TokenClass.java
// TokenClass enumeration definition
// TokenClass is an enumeration to represent lexical token classes in the
// Clite programming language.
public enum TokenClass {
EOF,
// keywords
PHP, PHPCLOSE, FUNCTION, RETURN, IF, ELSE, WHILE, FOREACH, AS, ARRAY, ARRAY_PUSH, PRINT, ARRAY_POP, NULL,
// punctuation
COMMA, SEMICOLON, PERIOD, COLON,
// operators
PLUS, MINUS, TIMES, SLASH, LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE, NOT, AND, OR, ASSIGN, EQU, EQ, LT, GT, NE, LE, GE,
// ids and literals
LITERAL, ID, INTEGER,
// variables
DOLLAR,
}
ErrorMessage.java
// ErrorMessage class
// This class prints error messages.
public class ErrorMessage {
public static void print (String message) {
System . out . println ("***** Error: " + message + " *****");
System . exit (0);
}
public static void print (int position, String message) {
System . out . println ("");
for (int i = 0; i < position; i++)
System . out . print (" ");
System . out . println ("^");
print (message);
}
}
CliteLex.java
// CliteLex class
// This class is a Clite lexical analyzer which reads a Clite source program
// and outputs the list of tokens comprising that program.
import java.io.InputStreamReader;
public class CliteLex {
private static final int MAX_TOKENS = 100;
public static void main (String args []) throws java.io.IOException {
int i, n;
Token [] token = new Token [MAX_TOKENS];
CLexer lexer = new CLexer (new InputStreamReader(System . in));
System . out . println ("Source Program");
System . out . println ("--------------");
System . out . println ();
n = -1;
do {
if (n < MAX_TOKENS)
token [++n] = lexer . nextToken ();
else
ErrorMessage . print (0, "Maximum number of tokens exceeded");
} while (token [n] . symbol () != TokenClass . EOF);
System . out . println ();
System . out . println ("List of Tokens");
System . out . println ("--------------");
System . out . println ();
for (i = 0; i < n; i++)
System . out . println (token [i]);
System . out . println ();
}
}
SyntaxAnalyzer.java - This is the code that should implement the EBNF Grammar that is given above.
So example:
SyntaxAnalyzer.java
CliteSyn.java - Nothing to edit here
// CliteSyn.java
// This program is a recursive descent parser for Clite.
public class CliteSyn {
public static void main (String args []) throws java.io.IOException {
System . out . println ("Source Program");
System . out . println ("--------------");
System . out . println ();
SyntaxAnalyzer clite = new SyntaxAnalyzer ();
clite . program ();
System . out . println ();
System . out . println ("PARSE SUCCESSFUL");
}
}
Test files:- Link:
http://www.cse.unt.edu/~4430s001/Test1.mphp
http://www.cse.unt.edu/~4430s001/Test2.mphp
http://www.cse.unt.edu/~4430s001/Test3.mphp
http://www.cse.unt.edu/~4430s001/Test4.mphp
Test1.mphp
Test2.mphp
Test3.mphp
Test4.mphp
Output should look like this for final output:
Explanation / Answer
The two errors that needs correction:
if (token . symbol () != TokenClass . PHP)
ErrorMessage . print (lexer . position (), "<?php EXPECTED"); getToken ();
if (token . symbol () != TokenClass . PHPEND)
ErrorMessage . print (lexer . position (), "?> EXPECTED"); getToken ();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.