-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
53 add option to parsejava to print ast in json #68
Changes from 25 commits
0734e66
00e878e
8d8b5cf
fa122d2
caf1594
94213cb
f6a67ef
2bcabca
299b2cf
390d6ca
5c52d8e
21bad33
1542328
4395892
4d8fb68
32adb59
4392582
fcbb81f
ad0e317
b96260c
3a2e60f
d0bc717
302f7cd
7c8eeba
07e545b
22b7f8d
f7e7837
38ec7d8
2856e54
fd9e674
f9790fc
08dc5e8
b4c1405
2399774
67b52f7
732a302
dc1f2ed
5fa2725
8c20da9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"exp":{"prim":{},"rands":{"expList":[{"lit":{"match":"LIT","str":"5","lno":1,"line":"+(5, 1)\n","eof":false}},{"lit":{"match":"LIT","str":"1","lno":1,"line":"+(5, 1)\n","eof":false}}]}}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"Program" : { | ||
"$type" : "Program", | ||
"exp" : { | ||
"$type" : "PrimappExp", | ||
"prim" : { | ||
"$type" : "AddPrim" | ||
}, | ||
"rands" : { | ||
"$type" : "Rands", | ||
"expList" : [ "java.util.ArrayList", [ { | ||
"$type" : "LitExp", | ||
"lit" : { | ||
"$type" : "Token", | ||
"match" : "LIT", | ||
"str" : "3", | ||
"lno" : 1, | ||
"line" : "+(3, 2)\n", | ||
"eof" : false | ||
} | ||
}, { | ||
"$type" : "LitExp", | ||
"lit" : { | ||
"$type" : "Token", | ||
"match" : "LIT", | ||
"str" : "2", | ||
"lno" : 1, | ||
"line" : "+(3, 2)\n", | ||
"eof" : false | ||
} | ||
} ] ] | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
skip WHITESPACE '\s+' | ||
skip COMMENT '%.*' | ||
LIT '\d+' | ||
LPAREN '\(' | ||
RPAREN '\)' | ||
COMMA ',' | ||
ADDOP '\+' | ||
SUBOP '\-' | ||
ADD1OP 'add1' | ||
SUB1OP 'sub1' | ||
VAR '[A-Za-z]\w*' | ||
% | ||
<program> ::= <exp> | ||
<exp>:LitExp ::= <LIT> | ||
<exp>:VarExp ::= <VAR> | ||
<exp>:PrimappExp ::= <prim> LPAREN <rands> RPAREN | ||
<rands> **= <exp> +COMMA | ||
<prim>:AddPrim ::= ADDOP | ||
<prim>:SubPrim ::= SUBOP | ||
<prim>:Add1Prim ::= ADD1OP | ||
<prim>:Sub1Prim ::= SUB1OP |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import java.util.*; | ||
import java.io.*; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; | ||
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; | ||
|
||
|
||
|
||
public class ParseJsonAst extends ProcessFiles { | ||
|
||
// Parse the program and call $ok() on the resulting parse tree | ||
public void action(Scan scn, Trace trace) { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | ||
objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE); | ||
objectMapper.enable(SerializationFeature.INDENT_OUTPUT); | ||
|
||
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator | ||
.builder() | ||
.allowIfBaseType(Object.class) // Can change "Object" to any type, we can look back at this later to be more specific | ||
.build(); | ||
|
||
objectMapper.activateDefaultTypingAsProperty(ptv, ObjectMapper.DefaultTyping.NON_FINAL, "$type"); | ||
|
||
_Start parseTree = _Start.parse(scn, trace); | ||
parseTree.$ok(); | ||
try { | ||
objectMapper.writeValue(new File("ASTroot.json"), parseTree); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
// Read programs from command-line files | ||
// and then read programs from standard input. | ||
public static void main(String [] args) { | ||
new ParseJsonAst().processFiles(args); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ | |
import io | ||
import shutil | ||
import tempfile | ||
|
||
argv = sys.argv[1:] # skip over the command-line argument | ||
|
||
# current file information | ||
|
@@ -113,9 +112,13 @@ def main(): | |
sem(nxt) # semantic actions | ||
|
||
def plccInit(): | ||
global flags, STD, STDT, STDP | ||
global flags, argv, STD, STDT, STDP | ||
STDT = ['ILazy','IMatch','IScan','ITrace', 'Trace', 'PLCCException', 'Scan'] | ||
STDP = ['ProcessFiles','Parse','Rep'] | ||
print(argv) | ||
if len(argv) > 1: | ||
if argv[0] == "--python": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plcc.py has a mechanism for handling command-line arguments. Unless there is a good reason, we should probably work within that framework. Specifically, there is a loop in main() that walks through all the command line arguments and calls processFlag() to populate a global dict with flags. The rest of the system interrogates flags to decide what to do. |
||
STDP.append('ParseJsonAst') | ||
STD = STDT + STDP | ||
STD.append('Token') | ||
# file-related flags -- can be overwritten | ||
|
@@ -140,7 +143,7 @@ def lex(nxt): | |
# turn off when all flags have been processed | ||
flagSwitch = True # turn off after all the flags have been processed | ||
for line in nxt: | ||
line = re.sub('\s+#.*', '', line) # remove trailing comments ... | ||
line = re.sub(r'\s+#.*', '', line) # remove trailing comments ... | ||
# NOTE: a token that has a substring like this ' #' will mistakenly be | ||
# considered as a comment. Use '[ ]#' instead | ||
line = line.strip() | ||
|
@@ -174,7 +177,7 @@ def qsub(match): | |
jpat = match.group(1) | ||
# print('>>> match found: jpat={}'.format(jpat)) | ||
return '' | ||
pat = "\s'(.*)'$" | ||
pat = r"\s'(.*)'$" | ||
# print('>>> q1 pat={}'.format(pat)) | ||
line = re.sub(pat, qsub, line) | ||
if jpat: | ||
|
@@ -184,7 +187,7 @@ def qsub(match): | |
# print('>>> q1 match found: line={} jpat={}'.format(line,jpat)) | ||
pass | ||
else: | ||
pat = '\s"(.*)"$' | ||
pat = r'\s"(.*)"$' | ||
# print('>>> q2 pat={}'.format(pat)) | ||
line = re.sub(pat, qsub, line) | ||
if jpat: | ||
|
@@ -1208,19 +1211,19 @@ def defangRHS(item): | |
return (tnt, field) | ||
|
||
def isID(item): | ||
return re.match('[a-z]\w*#?$', item) | ||
return re.match(r'[a-z]\w*#?$', item) | ||
|
||
def isNonterm(nt): | ||
debug('[isNonterm] nt={}'.format(nt)) | ||
if nt == 'void' or len(nt) == 0: | ||
return False | ||
return re.match('[a-z]\w*#?$', nt) | ||
return re.match(r'[a-z]\w*#?$', nt) | ||
|
||
def isClass(cls): | ||
return re.match('[A-Z][\$\w]*$', cls) or cls == 'void' | ||
return re.match(r'[A-Z][\$\w]*$', cls) or cls == 'void' | ||
|
||
def isTerm(term): | ||
return re.match('[A-Z][A-Z\d_$]*$', term) or term == '$LINE' | ||
return re.match(r'[A-Z][A-Z\d_$]*$', term) or term == '$LINE' | ||
|
||
def nt2cls(nt): | ||
# return the class name of the nonterminal nt | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
+(3, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, we should print the AST to stdout. Can we do that?