3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個贊
AST是CST的抽象(具體的語法樹或解析樹)。具體的語法樹是從用于語法分析文件的結(jié)果(語法)中得到的樹。因此,您的AST基本上是從您的語法定義派生的,但是已經(jīng)轉(zhuǎn)換為
Exp
/ | \
/ | \ *
Ident BinOp Ident into / \
/ | \ "x" "y"
/ | \
"x" * "y"
總而言之,我認(rèn)為您帖子中的示例看起來不錯。我可能會將變量聲明包裝在中varDeclList,將函數(shù)聲明包裝在中methDeclList,將return語句包裝在中stmtList。(見下文。)
蘋果在他的《 Java的現(xiàn)代編譯器實(shí)現(xiàn)》一書中描述了AST的某種“真實(shí)”表示。(可在此處找到資源。)
使用這些類,您的程序?qū)⑷缦滤荆?/p>
Program
ClassDeclList
ClassDecl
Identifier
id: Person
VarDeclList
VarDecl
type: String
id: name
VarDecl
type: int
id: age
MethDeclList
MethodDecl
modifiers: public
returnType: String
id: toString
Formals
(empty)
StmtList
returnStmt
Identifier
id: name
添加回答
舉報