最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

bison - Debugging AST and Parser Issues in My Custom Programming Language - Stack Overflow

programmeradmin2浏览0评论

I am developing my own programming language with a lexer, parser, and AST implementation. The AST seems to be constructed properly for most cases, but when testing with specific input, I encounter a syntax error, and the parser doesn’t behave as expected. Given the following input code for my language:

    whole_y a -> 5;
    floaty b -> 6;
    chain d -> ^ciao come stai^;
    throw_up 5;
    send_back 5;
    ciao(a,a);
    go
        as_long_as(a>5){ a->5; }
    all_set;

I get this error during parsing: Error at line 1: syntax error, unexpected T_RCURPAR, expecting ';' The parser should properly parse the as_long_as (while loop) block, but instead, it throws an error when encountering all_set; Here’s the relevant portion of my grammar:

block:
    T_GO control_block T_ALL_SET { $$ = node1(_CONTROLBLOCK, $2); }
    ;

control_block:
    if_block { $$ = $1; }
    | while_block { $$ = $1; }
    | for_block { $$ = $1; }
    | switch_case { $$ = $1; }
    ;

while_block:
    T_AS_LONG_AS T_LPAREN expression T_RPAREN T_LCURPAR statements T_RCURPAR { $$ = node2(_WHILE, $3, $6); }
    ;

statements:
    statement ';' { $$ = $1; }
    | statements statement ';' { $$ = node2(_STATEMENTS, $1, $2); }
    ;

statement:
    declaration { $$ = node1(_DECLARATION, $1); }
    | assignment { $$ = node1(_ASSIGNMENT, $1); }
    | function_call { $$ = $1; }
    | block { $$ = $1; }
    | T_SENDBACK expression { $$ = node1(_RETURN, $2); }
    | T_THROWUP expression { $$ = node1(_PRINT, $2); }
    ;

What could be causing the parser to throw a T_RCURPAR syntax error when it encounters all_set;?

  • Checked for missing or conflicting grammar rules.
  • Debugged the lexer to ensure tokens are being generated correctly.
  • Simplified the input to isolate the problematic grammar rule.
发布评论

评论列表(0)

  1. 暂无评论