Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am coding a Syntax Analyser in Java. Here is the BNF : The result is : I have already done the Lexical Analyser. Code

I am coding a Syntax Analyser in Java. Here is the BNF :

image text in transcribed

The result is :

image text in transcribed

I have already done the Lexical Analyser. Code is pasted in the end. Please use RDP and add as much Syntax Rules as you can to my current code, I'll finish the rest. Amend the code as needed.

My Lexical Analyer has three classes:

E.java (Main)

public class E {

public static void main(String[] args) {

L lexer = new L(System.getProperty("user.dir") + "\\input.txt");

System.out.println("Lexical Analysis Output");

System.out.println("token lexeme");

System.out.println("------------------------------------------------");

while (!lexer.isExhausted()) {

int strStartIndex = lexer.currentToken().toString().indexOf("_");

int strEndIndex = lexer.currentToken().toString().lastIndexOf("_");

String result = lexer.currentToken().toString().substring(strStartIndex + 1, strEndIndex);

System.out.printf("%-38s : %s ", result, lexer.currentLexema());

lexer.moveAhead();

}

if (lexer.isSuccessful()) {

System.out.println("------------------------------------------------");

System.out.println("Analysis Complete.");

} else {

System.out.println(lexer.errorMessage());

}

}

}

T.java (Token definition)

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public enum T {

T_KEYWORD_DEFINE ("define"),

T_KEYWORD_AS ("as"),

T_KEYWORD_INT ("int"),

T_KEYWORD_WHILE ("while"),

T_KEYWORD_END ("end"),

T_KEYWORD_RETURN ("return"),

T_KEYWORD_GET ("get"),

T_KEYWORD_PUT ("put"),

T_KEYWORD_IS ("is"),

T_KEYWORD_ENDIF ("ifend"),

T_KEYWORD_IF ("if"),

T_KEYWORD_THEN ("then"),

T_KEYWORD_ELSE ("else"),

T_KEYWORD_REAL ("real"),

T_KEYWORD_BOOLEAN ("boolean"),

T_KEYWORD_TRUE ("true"),

T_KEYWORD_FALSE ("false"),

T_KEYWORD_FUNCTION ("function"),

T_OPERATOR_LEG ("

T_OPERATOR_GEQ (">="),

T_OPERATOR_DIFFERENT (""),

T_OPERATOR_OR ("\\|"),

T_OPERATOR_EQ ("=="),

T_OPERATOR_NOT ("~"),

T_OPERATOR_EXCLAMATION ("!"),

T_OPERATOR_CARETEQ ("\\^="),

T_OPERATOR_EQGREAT ("=>"),

T_OPERATOR_EQLESS ("=

T_OPERATOR_MINUS ("-"),

T_OPERATOR_PLUS ("\\+"),

T_OPERATOR_DIV ("/"),

T_OPERATOR_ASSIGN ("="),

T_OPERATOR_LESS ("

T_OPERATOR_GT (">"),

T_SEPARATOR_CODESTART ("[$][$]"),

T_SEPARATOR_COLON (":"),

T_SEPARATOR_SEMI (";"),

T_SEPARATOR_COMMA (","),

T_SEPARATOR_OPENPARENTHESIS ("\\("),

T_SEPARATOR_CLOSEPARENTHESIS ("\\)"),

T_SEPARATOR_OPENBRACKET ("\\{"),

T_SEPARATOR_CLOSEBRACKET ("\\}"),

T_COMMENT_ ("\\[\\*[\\s\\S]*?\\*\\]|([^:]|^)\\/\\/.*$"),

T_SEPARATOR_OPENSQUAREBRACKET ("\\["),

T_SEPARATOR_CLOSESQUAREBRACKET ("\\]"),

T_OPERATOR_MUL ("\\*"),

T_REAL_ ("(-?\\d*)\\.(\\d+)?"),

T_INTEGER_ ("\\-?\\d+"),

T_ILLEGLEIDENTIFIER_ ("([A-Z]+[a-z]+[0-9]+)"),

T_ILLEGLESYMBOL_ ("[#|^|&]"),

T_IDENTIFIER_ ("([a-zA-Z]+\\d+[a-zA-Z]+)|([a-zA-Z]+[a-zA-Z]*)");

private final Pattern pattern;

T(String regex) {

pattern = Pattern.compile("^" + regex);

}

int endOfMatch(String s) {

Matcher m = pattern.matcher(s);

if (m.find()) {

return m.end();

}

return -1;

}

}

L.java (Lex processing)

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.HashSet;

import java.util.Set;

import java.util.stream.Stream;

public class L {

private StringBuilder input = new StringBuilder();

private T token;

private String lexema;

private boolean exhausted = false;

private String errorMessage = "";

private Set blankChars = new HashSet();

public L(String filePath) {

try (Stream st = Files.lines(Paths.get(filePath))) {

st.forEach(input::append);

} catch (IOException ex) {

exhausted = true;

errorMessage = "Could not read file: " + filePath;

return;

}

blankChars.add(' ');

blankChars.add(' ');

blankChars.add((char) 8);

blankChars.add((char) 9);

blankChars.add((char) 11);

blankChars.add((char) 12);

blankChars.add((char) 32);

moveAhead();

}

public void moveAhead() {

if (exhausted) {

return;

}

if (input.length() == 0) {

exhausted = true;

return;

}

ignoreWhiteSpaces();

if (findNextToken()) {

return;

}

exhausted = true;

if (input.length() > 0) {

errorMessage = "Undefined symbol: '" + input.charAt(0) + "'";

}

}

private void ignoreWhiteSpaces() {

int charsToDelete = 0;

while (blankChars.contains(input.charAt(charsToDelete))) {

charsToDelete++;

}

if (charsToDelete > 0) {

input.delete(0, charsToDelete);

}

}

private boolean findNextToken() {

for (T t : T.values()) {

int end = t.endOfMatch(input.toString());

if (end != -1) {

token = t;

lexema = input.substring(0, end);

input.delete(0, end);

return true;

}

}

return false;

}

public T currentToken() {

return token;

}

public String currentLexema() {

return lexema;

}

public boolean isSuccessful() {

return errorMessage.isEmpty();

}

public String errorMessage() {

return errorMessage;

}

public boolean isExhausted() {

return exhausted;

}

}

R1. SS $$ R2 - | R3- |Function> R4Function> ::= function ( R5. | R6 ::= | ::- : R8. .- int boolean real R9. ::= { } R10. ::= | R11. :-- Declaration>: Declaration List> e> ::= | , R 14. ::- *Statement> | Assign>| | R16. ::= { } R17. :- Expression> ; R18. :-if ( ) ifend | | | if ( ) else ifend R19. Return> ::= return ; | return . R20. := put ( ): R21. get (); R22. ::-while ( ) whileend R23. Expression> R25Expression> :- + |-Term> I Term> R26. - * | / | R27. :-- | | () | () > I Real true |false R1. SS $$ R2 - | R3- |Function> R4Function> ::= function ( R5. | R6 ::= | ::- : R8. .- int boolean real R9. ::= { } R10. ::= | R11. :-- Declaration>: Declaration List> e> ::= | , R 14. ::- *Statement> | Assign>| | R16. ::= { } R17. :- Expression> ; R18. :-if ( ) ifend | | | if ( ) else ifend R19. Return> ::= return ; | return . R20. := put ( ): R21. get (); R22. ::-while ( ) whileend R23. Expression> R25Expression> :- + |-Term> I Term> R26. - * | / | R27. :-- | | () | () > I Real true |false

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions