名前だけを探している場合は、次のように簡単です。
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
s や s を間違いなく間違えていることに注意してくださいComment
。しかし、それは簡単に修正できます。
上記の文法から生成されたレクサーは、2 種類のトークン ( Procedure
s とFunction
s) のみを生成し、残りの入力 (文字列リテラル、コメント、または何も一致しない場合は単一の文字: the .
) はレクサーからすぐに破棄されます (skip()
メソッド)。
次のような入力の場合:
some valid source
{
function NotAFunction ...
}
procedure Proc
Begin
...
End;
procedure Func
Begin
s = 'function NotAFunction!!!'
End;
次の解析ツリーが作成されます。
