我正在為運算式語法撰寫一個 LL(1) 決議器。我有以下語法:
E -> E E
E -> E - E
E -> E * E
E -> E / E
E -> INT
但是,這是左遞回,我使用以下語法洗掉了左遞回:
E -> INT E'
E' -> INT E'
E' -> - INT E'
E' -> * INT E'
E' -> / INT E'
E' -> ε
如果我有運算式1 2 * 3,決議器如何知道在加法之前評估乘法?
uj5u.com熱心網友回復:
嘗試這個:
; an expression is an addition
E -> ADD
; an addition is a multiplication that is optionally followed
; by - and another addition
ADD -> MUL T
T -> PM ADD
T -> ε
PM ->
PM -> -
; a multiplication is an integer that is optionally followed
; by */ and another multiplication
MUL -> INT G
G -> MD MUL
G -> ε
MD -> *
MD -> /
; an integer is a digit that is optionally followed by an integer
INT -> DIGIT J
J -> INT
J -> ε
; digits
DIGIT -> 0
DIGIT -> 1
DIGIT -> 2
DIGIT -> 3
DIGIT -> 4
DIGIT -> 5
DIGIT -> 6
DIGIT -> 7
DIGIT -> 8
DIGIT -> 9
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354819.html
上一篇:無論cookie在陣列中的位置如何,以相同的順序從cookie陣列中寫入cookie字串
下一篇:使用bash-c逐行決議
