我完全沒有想法。我今天花了每一分鐘的時間,但我完全沒有想法。解析器停止mid-parse
這是我Ocamlyacc
語法:
input: /* empty */ { }
| input stmt { }
stmt:
extern { print_endline "Got an extern import" }
| func { print_endline "Got function definition" }
| call { print_endline "Got function call" }
extern:
EXTERN proto { Extern $2 }
func:
DEF proto expr { Function ($2, $3) }
proto:
IDENTIFIER LPAREN id_list RPAREN { print_endline "Got prototype definition"; Prototype ($1, $3) }
id_list:
/* empty */ { [] }
| IDENTIFIER { [$1] }
| id_list COMMA IDENTIFIER { $3 :: $1 }
expr_list:
/* empty */ { [] }
| expr { [$1] }
| expr_list COMMA expr { $3 :: $1 }
expr:
call { $1 }
| expr OP expr { Binary ($2, $1, $3) }
| IDENTIFIER { Variable $1 }
| NUMBER { Number $1 }
| LPAREN expr RPAREN { $2 }
call:
IDENTIFIER LPAREN expr_list RPAREN { Call ($1, $3) }
當我開始分析def foo(a,b) a+b
應該告訴我它有一個功能和原型聲明,根據調試消息。但是,相反,我只收到解析proto
規則的消息。
進一步的調試消息顯示解析器會盡可能地與表達式a+b
的a
然後停止。沒有錯誤信息,沒有別的。它只是停止,如果整個文本帽子完全解析,而不符合stmt
中的任何規則。
沒有移位/減少錯誤或相似。 AST類型也不是問題。我不知道任何更多,也許別人可以幫助。當然,這是顯而易見的,但我看不到它。
編輯:詞法大衆的需求:
{
open Parser
}
rule token = parse
| [' ' '\t' '\n'] { token lexbuf }
| "def" { DEF }
| "extern" { EXTERN }
| "if" { IF }
| "then" { THEN }
| "else" { ELSE }
| ['+' '-' '*' '/'] as c { OP c }
| ['A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '0'-'9' '_']* as id { IDENTIFIER id }
| ['0'-'9']*'.'['0'-'9']+ as num { NUMBER (float_of_string num) }
| '(' { LPAREN }
| ')' { RPAREN }
| ',' { COMMA }
| '#' { comment lexbuf }
| _ { raise Parsing.Parse_error }
| eof { raise End_of_file }
and comment = parse
| '\n' { token lexbuf }
| _ { comment lexbuf }
看起來不錯。絕對沒有明顯的。 lexxer? – nlucaroni 2011-05-06 21:32:51