HABA explanation

HABA is a LALR(1) parser. When you press "Analyze" button, the lexical analysis and the syntactic analysis will be performed on the input string to create a syntax tree. It is converted into production rules, terminal symbols are extracted, and lexical analysis elements and dummy elements are obtained. Closures, state transitions, and parsing tables are also created from the production rules, and finally the grammar is output as a JavaScript program.

This page uses the following addition grammar as an input string as a sample.

Multi ::= Num ('+' Num)* ;
Num ::= "[0-9]+" ;
Space ::= "\s+" ;

"Ignore case" is not set here as it only affects the final JavaScript output.

Result and syntax tree

If all of the lexical analysis, syntactic analysis, and compilation are successful, "Success" is displayed in the result column. Otherwise, an error message will be displayed.

If the syntactic analysis is successful, even if the compilation fails, the syntax tree and other results are output and the "Display" check box will be clickable. Conversely, if there is an error in the syntactic analysis or the lexical analysis that precedes it, the "Display" check box remains disabled.

When you display the syntax tree, you see what follows the HABA grammar.

  • Gram
    • Rule
      • Name: Multi
      • ::=
      • Expr
        • List
          • Term
            • Fact
              • Name: Num
          • Term
            • Fact
              • Quot
                • (
                • Expr
                  • List
                    • Term
                      • Fact
                        • Fixd: '+'
                    • Term
                      • Fact
                        • Name: Num
                • )
            • Rept: *
      • ;
    • Rule
      • Name: Num
      • ::=
      • Expr
        • List
          • Term
            • Fact
              • Flex: "[0-9]+"
      • ;
    • Rule
      • Name: Space
      • ::=
      • Expr
        • List
          • Term
            • Fact
              • Flex: "\s+"
      • ;

Lexical analysis elements and dummy elements

The terminal symbols are extracted from the syntax tree and listed.

No.typeelement
1Fixed'+'
2RegExp"[0-9]+"

Valid terminal symbols are classified as lexical analysis elements and invalid terminal symbols are classified as dummy elements. Within the same classification, the fixed values are placed first and the regular expressions are placed later. Within the same type, they are arranged in the order they appear in the syntax tree. Identical elements are merged into one.

This allows the fixed value to be preferentially interpreted even if the fixed value and the regular expression match the same string. For example:

Variable ::= "[a-z]+" ;
IfState ::= 'if' Expr 'then' Expr ;
...

In this production rules, the word "if" can be interpreted as a keyword in the if statement rather than as a variable.

Production rules

Production rules are extracted from the syntax tree, expanded and listed.

No.expanded rule
0#0# ::= Multi ;
1Multi ::= Num #1# ;
2#1# ::= #1# '+' Num ;
3#1# ::= ;
4Num ::= "[0-9]+" ;

There can be multiple production rules for the start symbol (here Multi). In order to simplify the parsing process, a production rule using the new start symbol #0# is added to the beginning of the list.

The rule definition is only a concatenation of zero or more symbols. This is also to simplify the subsequent analysis process. The production rule entered as:

Multi ::= Num ('+' Num)* ;

is expanded as follows. The repeating part is extracted as another production rule.

Multi ::= Num #1# ;
#1# ::= ('+' Num)* ;

And expanding the repetition symbol from the second production rule,

Multi ::= Num #1# ;
#1# ::= #1# '+' Num ;
#1# ::= ;

there is no repetition, no alternative, no grouping, just concatenation.

The final production rule:

Space ::= "\s+" ;

has been removed because Space is an invalid non-terminal symbol that does not appear in other rule definitions.

In the rest of parsing process, we will use the five expanded production rules shown here, instead of the three input production rules. Notice that rule numbers start at 0.

The non-terminal symbols #0#, #1#, ... added by the expansion of production rules can not be entered on the screen (an error will occur even if they are entered), so they do not overlap with other non-terminal symbols.

Closures and state transitions

First, we create an LR(0) closures and its state transitions by a closure operation from the production rules. After that, all LR(0) items will be converted to LR(1) items by adding "next symbols" in order, and listed as a closure.

No.itemnext symbols
0#0# ::= • Multi$
Multi ::= • Num #1#$
Num ::= • "[0-9]+"'+' $
1#0# ::= Multi •$
2Multi ::= Num • #1#$
#1# ::= • #1# '+' Num$ '+'
#1# ::= •$ '+'
3Multi ::= Num #1# •$
#1# ::= #1# • '+' Num $ '+'
4#1# ::= #1# '+' • Num$ '+'
Num ::= • "[0-9]+"$ '+'
5#1# ::= #1# '+' Num •$ '+'
6Num ::= "[0-9]+" •$ '+'

• is the analysis position. Although $ is a terminal symbol that represents the end of a sentence, it also can not be entered, so it does not overlap with other terminal symbols.

The state transitions are as follows.

No.fromsymbolto
10Multi1
20Num2
32#1#3
43'+'4
54Num5
64"[0-9]+"6
70"[0-9]+"6

The from and to numbers are closure numbers. The state transition number has no meaning. In this example, there are 7 closures and 7 state transitions, which are the same number, but generally different numbers. Symbols can be terminal or non-terminal.

Parsing table

A parsing table is created based on closures and state transitions.

No.'+'"[0-9]+"$Multi#1#Num
0s6g1g2
1r0
2r3r3g3
3s4r1
4s6g5
5r2r2
6r4r4

This number is the same as the state number, or closure number. When the transition symbol is a terminal symbol, it is shifted to the next state, so the action symbol is s + the to number. When the transition symbol is a non-terminal symbol, it simply goes to the next state, so the action symbol is g + the to number.

If the analysis position of the item is at the right end of the production rule and no state transition occurs, reduction will be performed for the "next symbols". The action symbol in this case is r + rule number. In particular, the reduction of r0 represents acceptance. The starting state is 0.

JavaScript

The Grammar and Converter objects are created using the information provided so far.

// Grammar object
const Grammar = {

    "flag": "",

    "terminals": [
        "\\+",
        "[0-9]+",
    ],

    "dummies": [
        "\\s+",
    ],

    "rules": [
        "#0#=1",
        "Multi=2",
        "#1#=3",
        "#1#=0",
        "Num=1",
    ],

    "table": [
        [ "", "s6", "", "g1", "", "g2" ],
        [ "", "", "r0", "", "", "" ],
        [ "r3", "", "r3", "", "g3", "" ],
        [ "s4", "", "r1", "", "", "" ],
        [ "", "s6", "", "", "", "g5" ],
        [ "r2", "", "r2", "", "", "" ],
        [ "r4", "", "r4", "", "", "" ],
    ],

}

// Syntax converter
const Converter = {

    // Multi ::= Num ('+' Num)* ;
    "Multi": function(tree) {
    },

    // Num ::= "[0-9]+" ;
    "Num": function(tree) {
    },

}

If "Ignore case" is checked, the flag of the Grammar is "i". Otherwise, it is an empty string. All terminals are lexical analysis elements expressed in regular expressions, and dummies are dummy elements. Although the rules are production rules after expansion, the parsing uses only the number of symbols in the rule definition, so the numbers of concatenated symbols are output. The table is a parsing table.

The valid production rules that have entered are output to the Converter. Since only the function frame is created automatically, it is necessary to implement according to the language specification of the grammar. For example, you will write the following program.

// Syntax converter
const Converter = {

    // Multi ::= Num ('+' Num)* ;
    "Multi": function(tree) {
        tree.result = tree.children[0].result;
        for (let i = 2; i < tree.children.length; i += 2) {
            tree.result += tree.children[i].result;
        }
    },

    // Num ::= "[0-9]+" ;
    "Num": function(tree) {
        tree.result = parseInt(tree.children[0].text, 10);
    },

}

These programs can be parsed by HABA's own parser. Please check the sample page.