Drew Smith

Parser

Motivation

After experimenting with several C++ features I wanted to test them out. Particualarlly interesting to me was using variadic expressions to generate a lot of code. With this in mind, I wanted to create a parser which would create elements and insert them in thier final position, all while reducing object construction to a single (templated) method.

Challenges

I have never made a text processor before but I had the desire to learn. This parser is a shift reduce parser with one lookahead symbol. While I wanted to make the lookahead symbol entirely automated, that proved too difficult for the inital project and the lookahead symbols were/are manually specified.

Grammar

Here is the grammar for the language I created. It shares a C like curly brace syntax. Most of the Symbols used here are fairly self explanatory however the regex used can be found on github.



             <FunctionCall> ::= <FunctionParameters><CloseParenToken>
                              | <Identifier><OpenParenToken><CloseParenToken>

             <IfExpression> ::= <IfCondition><OpenBraceToken><Block><CloseBraceToken>

              <IfCondition> ::= <IfToken><OpenParenToken><AddExpression><CloseParenToken>

          <WhileExpression> ::= <WhileCondition><OpenBraceToken><Block><CloseBraceToken>

           <WhileCondition> ::= <WhileToken><OpenParenToken><AddExpression><CloseParenToken>

                  <Integer> ::= <IntegerToken>

               <Expression> ::= <Identifier>
                              | <Integer>
                              | <OpenParenToken><AddExpression><CloseParenToken>
                              | <FunctionCall>
                              | <Identifier><OpenSquareBraceToken><AddExpression><CloseSquareBraceToken>
                              | <StringLiteralToken>

           <MultExpression> ::= <MultExpression><MultToken><Expression>
                              | <Expression>

            <AddExpression> ::= <AddExpression><AddToken><MultExpression>
                              | <AddExpression><SubToken><MultExpression>
                              | <MultExpression>

               <Assignment> ::= <Identifier><EqlToken><AddExpression>
                              | <Identifier><OpenSquareBraceToken><AddExpression><CloseSquareBraceToken><EqlToken><AddExpression>

                <Statement> ::= <AddExpression><SemicolonToken>
                              | <Assignment><SemicolonToken>
                              | <IfExpression>
                              | <WhileExpression>

                    <Block> ::= <Block><Statement>
                              | <Statement>

       <FunctionParameters> ::= <Identifier><OpenParenToken><AddExpression>
                              | <FunctionParameters><CommaToken><AddExpression>

Interactive terminal

The only function defined is the print function, which prints data to the screen.