问题描述
有什么通用的解决方案如何使用不完整的语法?就我而言,我只想检测Delphi(Pascal)文件中的方法,这意味着procedures
和functions
.以下第一次尝试有效
Are there any common solutions how to use incomplete grammars? In my case I just want to detect methods in Delphi (Pascal)-files, that means procedures
and functions
. The following first attempt is working
methods
: ( procedure | function | . )+
;
但这是一个解决方案吗?有更好的解决方案吗?是否可以通过某个动作停止解析(例如,在检测到implementation
之后).使用预处理程序有意义吗?当是的时候-怎么办?
but is that a solution at all? Are there any better solutions? Is it possible to stop parsing with an action (e. g. after detecting implementation
). Does it make sense to use a preprocessor? And when yes - how?
推荐答案
如果您只是在寻找姓名,那么就这么简单:
If you're only looking for names, then something as simple as this:
grammar PascalFuncProc;
parse
: (Procedure | Function)* EOF
;
Procedure
: 'procedure' Spaces Identifier
;
Function
: 'function' Spaces Identifier
;
Ignore
: (StrLiteral | Comment | .) {skip();}
;
fragment Spaces : (' ' | '\t' | '\r' | '\n')+;
fragment Identifier : ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9')*;
fragment StrLiteral : '\'' ~'\''* '\'';
fragment Comment : '{' ~'}'* '}';
可以解决问题.请注意,我对Delhpi/Pascal不太熟悉,因此我肯定会搞砸StrLiteral
和/或Comment
,但这很容易解决.
will do the trick. Note that I am not very familiar with Delhpi/Pascal, so I am surely goofing up StrLiteral
s and/or Comment
s, but that'll be easily fixed.
从上面的语法生成的词法分析器将只产生两种类型的标记(Procedure
s和Function
s),其余的输入(字符串文字,注释或如果没有匹配则为单个字符): .
)将立即从词法分析器中丢弃(skip()
方法).
The lexer generated from the grammar above will only produce two type of tokens (Procedure
s and Function
s), the rest of the input (string literals, comments or if nothing is matched, a single character: the .
) is being discarded from the lexer immediately (the skip()
method).
对于这样的输入:
some valid source
{
function NotAFunction ...
}
procedure Proc
Begin
...
End;
procedure Func
Begin
s = 'function NotAFunction!!!'
End;
将创建以下分析树:
这篇关于解析不完整的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!