Created
November 23, 2018 05:28
-
-
Save MajorTom3K1M/49b005be2ca16853dd4abad1a2e97c6d 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 = (\/\/)[\tA-Za-z0-9_ ]* | (\/\*)[\t\sA-Za-z0-9_ ]*(\*\/) | |
String = (\")[\tA-Za-z0-9_ ]*(\") | |
Separator = "("|")"|";" | |
Whitespace = \s+ | |
%{ | |
List<String> identifiers = new ArrayList(); | |
%} | |
%% | |
{Numbers} {System.out.printf("integer : %s \n", yytext());} | |
{Keywords} {System.out.printf("keyword : %s \n", yytext());} | |
{Identifier} { | |
if(!identifiers.contains(yytext())) { | |
System.out.printf("new identifier : %s \n", 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