我有一個Jison一段代碼,看起來像這樣:如何在Jison中檢測新行?
%lex
%options flex
%{
if (!('regions' in yy)) {
yy.regions = [];
}
%}
text [a-zA-Z][a-zA-Z0-9]*
%%
\s+ /* skip whitespace */
\n+ return 'NL';
"," return ',';
"-" return '-';
"[" return '[';
"]" return ']';
{text} return 'TEXT';
<<EOF>> return 'EOF';
/lex
%start expressions
%%
expressions
: content EOF
{
console.log(yy.regions);
return yy.regions;
}
| EOF
{
console.log("empty file");
return yy.regions;
}
;
content
: line NL content
{ console.log("NL"); }
| line content
{ console.log("no NL"); }
//| line NL
// { console.log("parsing line with NL"); }
| line
{ console.log("parsing line"); }
;
line
: '[' text ']'
{ yy.regions.push($2); $$ = $2; }
;
text
: TEXT
{ $$ = $1; }
;
這是我的輸入看起來像此刻(我從我計劃有,我想建立最基本的結構開始它從那裏):
[sectionA]
[sectionB]
[sectionC]
我遇到的問題是,沒有檢測到新行。它總是進入line content
,永遠不會進入line NL content
。後來我想分析的東西,看起來更像是這樣的:
[sectionA]
something1, something2, something3
something4, something5, something6
[sectionB]
something4, something5, something6
[sectionC]
something4, something5, something6
something4, something5, something6
something4, something5, something6
在未來,這會變得有點複雜,但我最初的想法是一種打破它每行的基礎上(新行在許多情況下將作爲分隔符)。我對這個東西完全陌生,所以我可能對如何解決這個問題有一個完全錯誤的想法。所以我的問題是如何檢測新的線?此外,如果有更好的方法來處理我想要做的事,任何建議都是值得歡迎的。謝謝。
它工作。非常感謝。它完全符合我的需求,而且答案通常很豐富。 – pootzko