Last active
November 14, 2018 10:04
-
-
Save MajorTom3K1M/33ed4743b7f956be0a27d0b3009304b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
%% | |
%class lexical | |
%standalone | |
%unicode | |
%line | |
%column | |
LineTerminator = \r|\n|\r\n | |
Numbers = [0-9]+ | |
Keywords = "if"|"then"|"else"|"endif"|"while"|"do"|"endwhile"|"print"|"newline"|"read" | |
Identifier = [A-Za-z0-9_]+ | |
Operator = "="|"+"|"-"|"*"|"/" | |
Comment = (\/\/)[A-Za-z0-9_ ]* | (\/\*)[A-Za-z0-9_ ]*(\*\/) | |
String = (\")[A-Za-z0-9_ ]*(\") | |
Separator = "("|")"|";" | |
Whitespace = \s+ | |
%{ | |
List<String> keywords = new ArrayList(); | |
List<String> identifiers = new ArrayList(); | |
%} | |
%% | |
{Numbers} {System.out.printf("integer : %s \n", yytext());} | |
{Keywords} {System.out.printf("keyword : %s \n", yytext()); | |
keywords.add(yytext()); | |
} | |
{Identifier} { | |
if(!identifiers.contains(yytext())) { | |
System.out.printf("new identifier : %s \n", yytext()); | |
if(!keywords.contains(yytext())){ | |
identifiers.add(yytext()); | |
} | |
} else { | |
System.out.printf("identifier : %s already in symbol table \n", yytext()); | |
} | |
} | |
{Operator} {System.out.printf("operator : %s \n", yytext());} | |
{String} {System.out.printf("string : %s \n", yytext());} | |
{Comment} {System.out.printf("comments : %s \n", yytext());} | |
{Separator} {System.out.printf("separator : %s \n", yytext());} | |
{LineTerminator} {} | |
{Whitespace} {} | |
. { | |
System.out.printf("Error \n"); | |
System.exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment