簡體   English   中英

關於LL1非遞歸解析器中的AST構造

[英]About AST construction in LL1 non recursive parser

我已經在非遞歸方法中使用顯式堆棧實現了LL1解析器。

以下算法來自《龍書》:

set zp to point to the first symbol of w;
set X to the top stack symbol;
while ( X != $ ) { /* stack is not empty */
    if ( X is a ) 
       pop the stack and advance zp;
    else if ( X is a terminal )
       error();
    else if ( M[X, a] is an error entry )
       error();
    else if ( M[X,a] = X -+ Y1Y2 Yk ) {
       output the production X -+ YlY2 - . Yk;
       pop the stack;
       push Yk, Yk-1,. . . , Yl onto the stack, with Yl on top;

    set X to the top stack symbol;
}

這本書說:

解析器由一個程序控制,該程序考慮X(位於堆棧頂部的符號)和a(當前輸入符號)。 如果X是非終結符,則解析器通過查詢解析表IM的條目M [X,a]選擇X生成。 (此處可以執行其他代碼,例如,在解析樹中構造節點的代碼。)否則,它將檢查端子X與當前輸入符號a之間的匹配。

但是,我需要有關如何在這種方法下構造表達式樹節點的更多信息。 我有UnaryOperator,BinaryOperator等的節點層次結構,但不知道在哪里實例化它。

但是我還沒有找到任何簡單的例子(例如算術語言)。

我一直在尋找相同的信息-如何使用LL(1)表驅動的非遞歸解析來創建解析樹-但發現的很少。

剛才我發現了這些講義https://parasol.tamu.edu/~rwerger/Courses/434/lec7.pdf ,其中建議對於每個終端或非終端,還將相應的解析樹節點也壓入堆棧。 不過,這似乎確實很浪費。 這是偽代碼中提出的算法:

TOS ← 0
Stack[tos++] ← eof
Stack[tos++] ← *root node*
Stack[tos++] ← *Start Symbol*
token ← next_token()
X ← Stack[tos]
repeat
  if X is a terminal or eof then
    if X = token then
      pop X
      token ← next_token()
      *pop and fill in node*
    else error()
  else /* X is a non-terminal */
    if M[X,token] = X → Y1Y2...Yk then
      pop X
      *pop node for X
      build node for each child and
      make it a child of node for X*
      push nk,Yk,nk-1,Yk-1...n1,Y1
    else error()
until X = eof

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM