From c5c9bb2f0d177dc9a983d7fa5bd3e2529117ee54 Mon Sep 17 00:00:00 2001 From: bill Date: Sat, 20 Feb 2021 19:46:30 +0800 Subject: [PATCH] Changes made on Flex to adapt for merging. --- src/main/jflex/chocopy/pa1/ChocoPy.jflex | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/jflex/chocopy/pa1/ChocoPy.jflex b/src/main/jflex/chocopy/pa1/ChocoPy.jflex index 8ecbb02..8699d5c 100644 --- a/src/main/jflex/chocopy/pa1/ChocoPy.jflex +++ b/src/main/jflex/chocopy/pa1/ChocoPy.jflex @@ -34,6 +34,7 @@ import java.util.ArrayList; final ComplexSymbolFactory symbolFactory = new ComplexSymbolFactory(); private int currIndent = 0; //Current Indentation Level private String currString = ""; + private int str_l = 0, str_c = 0; //Start location of a string. /*A stack that keeps track of the spaces in each Indentation Level*/ private ArrayList stack = new ArrayList(20); /** Return a terminal symbol of syntactic category TYPE and no @@ -45,6 +46,7 @@ import java.util.ArrayList; /** Return a terminal symbol of syntactic category TYPE and semantic * value VALUE at the current source location. */ private Symbol symbol(int type, Object value) { + //System.out.println(yytext() + ChocoPyTokens.terminalNames[type]); return symbolFactory.newSymbol(ChocoPyTokens.terminalNames[type], type, new ComplexSymbolFactory.Location(yyline + 1, yycolumn + 1), new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()), @@ -134,6 +136,7 @@ if True: AFTER state. */ pop(); + //System.out.println("dedent"); return symbol(ChocoPyTokens.DEDENT, currIndent); } /*Otherwise, we will start dealing with the rest @@ -146,6 +149,8 @@ if True: current level should have, start a new level which will have `currIndent' indents. */ + //System.out.println("indent"); + push(currIndent); return symbol(ChocoPyTokens.INDENT, currIndent); } @@ -161,7 +166,7 @@ if True: /* Literals. */ {IntegerLiteral} { return symbol(ChocoPyTokens.NUMBER, Integer.parseInt(yytext())); } - "\"" {yybegin(STR); currString = "";} //Start taking a string when see a " + "\"" {yybegin(STR); str_l = yyline + 1; str_c = yycolumn + 1; currString = "";} //Start taking a string when see a " "False" { return symbol(ChocoPyTokens.BOOL, false); } "True" { return symbol(ChocoPyTokens.BOOL, true); } "None" { return symbol(ChocoPyTokens.NONE); } @@ -223,7 +228,7 @@ if True: /*Identifiers*/ - {Identifiers} {return symbol(ChocoPyTokens.ID, yytext());} + {Identifiers} {return symbol(ChocoPyTokens.ID, yytext());} /* Whitespace. */ {WhiteSpace} { /* ignore */ } /* Comment. */ @@ -232,9 +237,12 @@ if True: { {StringLiteral} {currString += yytext();} \\$ { /*'\' at the end of line, do nothing.*/ } - "\"" {yybegin(AFTER); return symbol(ChocoPyTokens.STRING, currString);} // accepted a ", return to AFTER state + "\"" {yybegin(AFTER); return symbolFactory.newSymbol(ChocoPyTokens.terminalNames[ChocoPyTokens.STRING], ChocoPyTokens.STRING, + new ComplexSymbolFactory.Location(str_l, str_c), + new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()), + currString);} // accepted a ", return to AFTER state } -<> { return symbol(ChocoPyTokens.EOF); } +<> { if(!stack.isEmpty()){ return symbol(ChocoPyTokens.DEDENT, pop());} return symbol(ChocoPyTokens.EOF);} /* Error fallback. */ [^] { return symbol(ChocoPyTokens.UNRECOGNIZED); }