[英]Parser grammar for these expresisons
I am trying to come up with a simple EBNF grammar for parsing the below expressions examples: ("->" isn't part of input). 我试图提出一个简单的EBNF语法来解析以下表达式示例:(“->”不是输入的一部分)。 'a', 'b' 'c' are multi character words which contain alphabets only. “ a”,“ b”,“ c”是仅包含字母的多字符单词。
-> a
-> (a)
-> (a | b | c)
-> (a (b | c | d))
-> (a (b | (c | d)) )
-> (a (
b (
c x
| d (e | f)
)
)
)
I tried coming up with several grammars (in Antlr) but all of them break for a few cases, this is what I have right now, Any help would be appreciated. 我尝试提出几种语法(在Antlr中),但是它们在少数情况下都无法使用,这就是我现在所拥有的,任何帮助将不胜感激。
term: '(' WORD+ options_list ')'
| '(' WORD+ ('|' term)* ')'
| WORD+
;
options_list: '(' term ('|' term)* ')'
;
}
WORD : ('a'..'z')+
;
EDIT 1: Basically I am trying to create a hierarchical tree of words from the grammar for example in the last case the tree would look like these 编辑1:基本上我想从语法创建单词的分层树,例如在最后一种情况下,树看起来像这些
[a]
|
[b]
/ \
[c,x] [d]
/ \
[e] [f]
I don't want depend on Antlr or JavaCC for auto generating the parser for a different set of reasons, I would want to write my own simple recursive descent parser in order to do that I am first coming up with a valid grammar that works for all cases, then it would simple enough to implement from that. 我不希望出于不同的原因而依赖Antlr或JavaCC自动生成解析器,我想编写自己的简单递归下降解析器,以便首先实现有效的语法,所有情况下,从中实施起来就足够简单了。
EDIT2: After working some bits I have this which seems to work for EDIT2:工作了一些位后,我似乎对此工作
term: WORD term?
| '(' term ('|' term)* ')'
;
WORD : ('a'..'z')+
;
The following grammar appears to parse all your provided examples. 以下语法似乎可以解析您提供的所有示例。
term : WORD+
| '(' alt_term ')'
;
alt_term : app_term
| app_term '|' alt_term
| app_term '|' '(' alt_term ')'
;
app_term : WORD+
| WORD+ '(' alt_term ')'
;
WORD : ('a'..'z')+
;
I tested it in AntlrWorks, and all of your examples are parsed. 我在AntlrWorks中对其进行了测试,并对您的所有示例进行了解析。 I just added EOF
recognition and skip
on whitespaces. 我刚刚添加了EOF
识别,并skip
了空白。
Analysis of each case: 每种情况的分析:
a
1.1. 1.1。 term
(1) term
(1)
(a)
2.1. 2.1。 term
(2) term
(2)
2.2. 2.2。 alt_term
(1) alt_term
(1)
2.3. 2.3。 app_term
(1) app_term
(1)
(a | b | c)
3.1. 3.1。 term
(2) term
(2)
3.2. 3.2。 alt_term
(2) alt_term
(2)
3.3. 3.3。 app_term
(1) app_term
(1)
3.4. 3.4。 alt_term
(2) alt_term
(2)
3.5. 3.5。 app_term
(1) app_term
(1)
3.6. 3.6。 alt_term
(1) alt_term
(1)
3.7. 3.7。 app_term
(1) app_term
(1)
(a (b | c | d))
4.1. 4.1。 term
(2) term
(2)
4.2. 4.2。 alt_term
(1) alt_term
(1)
4.3. 4.3。 app_term
(2) app_term
(2)
4.4. 4.4。 alt_term
(2) alt_term
(2)
4.5. 4.5。 app_term
(1) app_term
(1)
4.6. 4.6。 alt_term
(2) alt_term
(2)
4.7. 4.7。 app_term
(1) app_term
(1)
4.8. 4.8。 alt_term
(1) alt_term
(1)
4.9. 4.9。 app_term
(1) app_term
(1)
(a (b | (c | d)) )
5.1. 5.1。 term
(2) term
(2)
5.2. 5.2。 alt_term
(1) alt_term
(1)
5.3. 5.3。 app_term
(2) app_term
(2)
5.4. 5.4。 alt_term
(3) alt_term
(3)
5.5. 5.5。 app_term
(1) app_term
(1)
5.6. 5.6。 alt_term
(2) alt_term
(2)
5.7. 5.7。 app_term
(1) app_term
(1)
5.8. 5.8。 alt_term
(1) alt_term
(1)
5.9. 5.9。 app_term
(1) app_term
(1)
(a (b (cx | d (e | f))))
6.1. 6.1。 term
(2) term
(2)
6.2. 6.2。 alt_term
(1) alt_term
(1)
6.3. 6.3。 app_term
(2) app_term
(2)
6.4. 6.4。 alt_term
(1) alt_term
(1)
6.5. 6.5。 app_term
(2) app_term
(2)
6.6. 6.6。 alt_term
(2) alt_term
(2)
6.7. 6.7。 app_term
(1) app_term
(1)
6.8. 6.8。 alt_term
(1) alt_term
(1)
6.9. 6.9。 app_term
(2) app_term
(2)
6.10. 6.10。 alt_term
(2) alt_term
(2)
6.11. 6.11。 app_term
(1) app_term
(1)
6.12. 6.12。 alt_term
(1) alt_term
(1)
6.13. 6.13。 app_term
(1) app_term
(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.