Anglr Language Syntax Rules


Introduction

This paragraph contains complete set of syntax rules for Anglr Language using nested rules. This is an attractive example, which presents, how to construct the whole grammar of some programming language using only nested rules. The only standalone rule is starting rule <anglr source> This grammar is equivalent to grammar composed with standalone syntax rules, since they derive the same languages.

[ Description Text='General settings' Hover='true' ]
[ CompilationInfo NameSpace='Anglr' CodeDir='.' Hover='true' ]
%general anglrSettings
%{
%}

[ Description Text='Declarations' Hover='true' ]
[ CompilationInfo ClassName='AnglrDeclarations' NameSpace='Anglr.Declarations' Access='public' Hover='true' ]
%declarations anglrDecls
%{

[
    Description Hover='true'
    Text='regular expressions representing constant terminal identifiers'
]

    %regex
    {
        vertical-bar           \|
        comma                  \,
        colon                  \:
        semicolon              \;
        left-curly-bracket     \{
        right-curly-bracket    \}
        left-bracket           \(
        right-bracket          \)
        left-scanner-bracket   \%\{
        right-scanner-bracket  \%\}
        left-square-bracket    \[
        right-square-bracket   \]
        at-sign                \@
        double-at-sign         \@\@
        equals-sign            \=
        question-mark          \?
        plus-sign              \+
        minus-sign             \-
        asterisk               \*
        slash                  \/
        inv-plus-sign          \~\+
        inv-minus-sign         \~\-
        inv-asterisk           \~\*
        inv-slash              \~\/
    }

[
    Description Hover='true'
    Text='regular expressions representing anglr directives'
]

    %regex
    {
        empty         %[ \t]*empty
        terminal      %[ \t]*terminal
        general       %[ \t]*general
        declarations  %[ \t]*declarations
        rregex        %[ \t]*regex
        scanner       %[ \t]*scanner
        lexer         %[ \t]*lexer
        parser        %[ \t]*parser
        skip          skip
        ttoken        terminal
        revent        event
        push          push
        pop           pop
    }

[
    Description Hover='true'
    Text='regular expressions representing'
    Text='- identifier names'
    Text='- character strings'
    Text='- decimal numbers'
]

    %regex
    {
        letter        [a-zA-Z_]
        digit         [0-9]
        identifier    {letter}({letter}|{digit}|[-\.])*|<{letter}({letter}|{digit}|[-\. ])*>
        cstring       \"([^"]|\"\")*\"|\'([^']|\'\')*\'
        number        {digit}+
    }

[
    Description Hover='true'
    Text='all regular expressions together'
]

    %regex lexical-unit  {vertical-bar}|{comma}|{colon}|{semicolon}|{left-curly-bracket}|{right-curly-bracket}|{left-scanner-bracket}|{right-scanner-bracket}|{left-square-bracket}|{right-square-bracket}|{equals-sign}|{empty}|{terminal}|{declarations}|{general}|{rregex}|{scanner}|{parser}|{identifier}|{cstring}

[
    Description Hover='true'
    Text='terminal identifiers. Most of them are specified with actual string representations'
]

    %terminal
    {
        <vertical bar> '|'
        <comma> ','
        <left curly bracket> '{'
        <right curly bracket> '}'
        <left bracket> '('
        <right bracket> ')'
        <left part bracket> '%{'
        <right part bracket> '%}'
        <left square bracket> '['
        <right square bracket> ']'
        <double at sign> '@@'
        <at sign> '@'
        <equals sign> '='
        <colon> ':'
        <semicolon> ';'
        <question mark> '?'
        <plus sign> '+'
        <minus sign> '-'
        <asterisk> '*'
        <slash> '/'
        <inv plus sign> '~+'
        <inv minus sign> '~-'
        <inv asterisk> '~*'
        <inv slash> '~/'
        <any>
        <cstring>
        <empty> '%empty'
        <identifier>
        <terminal> '%terminal'
        <general> '%general'
        <declarations> '%declarations'
        <regex> '%regex'
        <scanner> '%scanner'
        <lexer> '%lexer'
        <parser> '%parser'
        <regular expression>
        <number>
        <skip> 'skip'
        <ttoken> 'terminal'
        <event> 'event'
        <push> 'push'
        <pop> 'pop'
    }

[
    Description Hover='true'
    Text='The following terminals are used in <anglr soure> syntax rule and are associated'
    Text='with different fragments of anglr file. They have no textual representation. They'
    Text='are always used to distinguish fragments of anglr file. They shall be used by first'
    Text='indicating the terminal symbol followed by the text of the fragment. Examples are'
    Text='all productions of <anglr source> syntax rule. The lexical and yntax analysers must'
    Text='address these rules in a specific way:'
    Text=' 1) the task of the leading terminal is that the syntax analyzer correctly selects'
    Text='    the production of <anglr soure> syntax rule by which to analyze the text'
    Text='    belonging to the fragment.'
    Text=' 2) The lexical analyser must generate the leading terminal on the first run, enabling'
    Text='    the syntax analyzer to make the right decision when selecting the rule to use for'
    Text='    the analysis of the fragment. Other terminal symbols are obtained in the usual'
    Text='    way by lexical analysis of the text belonging to the fragment.'
]

    %terminal
    {
        <anglr file terminal>
        <anglr file part list terminal>
        <anglr file part terminal>
        <general part terminal>
        <declaration part terminal>
        <scanner part terminal>
        <regular expression list terminal>
        <regular expression usage terminal>
        <actions terminal>
        <action terminal>
        <skip action terminal>
        <terminal action terminal>
        <event action terminal>
        <push action terminal>
        <pop action terminal>
        <lexer part terminal>
        <parser part terminal>
        <attribute list terminal>
        <attribute terminal>
        <name value list terminal>
        <name value pair terminal>
        <anglr definition list terminal>
        <anglr definition with attribute list terminal>
        <anglr definition terminal>
        <single terminal definition terminal>
        <single regex definition terminal>
        <block of terminal definitions terminal>
        <block of regex definitions terminal>
        <terminal definition terminal>
        <regex definition terminal>
        <block terminal definitions terminal>
        <block terminal definition terminal>
        <block regex definitions terminal>
        <block regex definition terminal>
        <anglr syntax rule list terminal>
        <anglr syntax rule terminal>
        <anglr syntax production list terminal>
        <anglr syntax production terminal>
        <anglr nested rule terminal>
        <anglr syntax production list name terminal>
        <name list terminal>
        <production name terminal>
        <marker list terminal>
        <marker terminal>
        <g name terminal>
        <name terminal>
        <cardinality delimiter terminal>
        <cardinality terminal>
        <delimiter terminal>
    }

%}

[ Description Text='Scanner for multi-line comment' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='CommentRegex' NameSpace='Anglr.RegexLib' Access='public' Hover='true' ]
%scanner comment_ctx
%{
\*\/
        pop
[\n\r]
        skip
.
        skip
%}

[ Description Text='Scanner for hidden attribute' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='AttributeRegex' NameSpace='Anglr.RegexLib' Access='public' Hover='true' ]
%scanner attribute_ctx
%{
\]\]
        pop
{identifier}
        skip
{equals-sign}
        skip
{cstring}
        skip
[\n\r]
        skip
.
        skip
%}

[ Description Text='Scanner for %scanner directive' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='ScannerIdRegex' NameSpace='Anglr.RegexLib' Access='public' Hover='true' ]
%scanner scanner_id_ctx
%{
\/\*
        push comment_ctx
\[\[
        push attribute_ctx
\/\/[^\n]*
        skip
{identifier}
        push scanner_part_ctx
        terminal <identifier>
[ \t]+
        skip
[\n\r]
        skip
%}

[ Description Text='Scanner for body of %scanner directive (text between %{ and %} )' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='ScannerPartRegex' NameSpace='Anglr.RegexLib' Access='public' Hover='true' ]
%scanner scanner_part_ctx
%{
\/\*
        push comment_ctx
\[\[
        push attribute_ctx
\/\/[^\n]*
        skip
{left-scanner-bracket}
        terminal <left part bracket>
{right-scanner-bracket}
        pop
        pop
        terminal <right part bracket>
{skip}
        terminal <skip>
{ttoken}
        terminal <ttoken>
{revent}
        terminal <event>
{push}
        terminal <push>
{pop}
        terminal <pop>
{identifier}
        terminal <identifier>
[ \t]+
        skip
.+
        terminal <regular expression>
[\n\r]
        skip
%}

[ Description Text='Scanner for %regex directive' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='RegexIdRegex' NameSpace='Anglr.RegexLib' Access='public' Hover='true' ]
%scanner regex_id_ctx
%{
\/\*
        push comment_ctx
\[\[
        push attribute_ctx
\/\/[^\n]*
        skip
{identifier}
        push regex_part_ctx
        terminal <identifier>
{left-curly-bracket}
        push regex_block_ctx
        terminal <left curly bracket>
{right-scanner-bracket}
        pop
        terminal <right part bracket>
[ \t]+
        skip
[\n\r]
        skip
%}

[ Description Text='Scanner for body of %regex directive' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='RegexPartRegex' NameSpace='Anglr.RegexLib' Access='public' Hover='true' ]
%scanner regex_part_ctx
%{
\/\*
        push comment_ctx
\[\[
        push attribute_ctx
\/\/[^\n]*
        skip
[ \t]+
        skip
.+
        pop
        pop
        terminal <regular expression>
[\n\r]
        skip
%}

[ Description Text='Scanner for body of %regex directive' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='RegexBlockRegex' NameSpace='Anglr.RegexLib' Access='public' Hover='true' ]
%scanner regex_block_ctx
%{
\/\*
        push comment_ctx
\[\[
        push attribute_ctx
\/\/[^\n]*
        skip
[ \t\n]+
        skip
{right-curly-bracket}
        pop
        pop
        terminal <right curly bracket>
{identifier}
        push regex_block_part_ctx
        terminal <identifier>
%}

[ Description Text='Scanner for body of %regex directive' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='RegexBlockPartRegex' NameSpace='Anglr.RegexLib' Access='public' Hover='true' ]
%scanner regex_block_part_ctx
%{
\/\*
        push comment_ctx
\[\[
        push attribute_ctx
\/\/[^\n]*
        skip
[ \t]+
        skip
.+
        pop
        terminal <regular expression>
[\n\r]
        skip
%}

[ Description Text='Scanner for anglr file' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='AnglrRegex' NameSpace='Anglr.RegexLib' Access='public' Hover='true' ]
%scanner anglrScanner
%{
\/\*
        push comment_ctx
\[\[
        push attribute_ctx
\/\/[^\n]*
        skip
{vertical-bar}
        terminal <vertical bar>
{comma}
        terminal <comma>
{colon}
        terminal <colon>
{semicolon}
        terminal <semicolon>
{left-curly-bracket}
        terminal <left curly bracket>
{right-curly-bracket}
        terminal <right curly bracket>
{left-bracket}
        terminal <left bracket>
{right-bracket}
        terminal <right bracket>
{left-scanner-bracket}
        terminal <left part bracket>
{right-scanner-bracket}
        terminal <right part bracket>
{left-square-bracket}
        terminal <left square bracket>
{right-square-bracket}
        terminal <right square bracket>
{equals-sign}
        terminal <equals sign>
{double-at-sign}
        terminal <double at sign>
{at-sign}
        terminal <at sign>
{question-mark}
        terminal <question mark>
{plus-sign}
        terminal <plus sign>
{minus-sign}
        terminal <minus sign>
{asterisk}
        terminal <asterisk>
{slash}
        terminal <slash>
{inv-plus-sign}
        terminal <inv plus sign>
{inv-minus-sign}
        terminal <inv minus sign>
{inv-asterisk}
        terminal <inv asterisk>
{inv-slash}
        terminal <inv slash>
{empty}
        terminal <empty>
{terminal}
        terminal <terminal>
{general}
        terminal <general>
{declarations}
        terminal <declarations>
{rregex}
        push regex_id_ctx
        terminal <regex>
{scanner}
        push scanner_id_ctx
        terminal <scanner>
{lexer}
        terminal <lexer>
{parser}
        terminal <parser>
{identifier}
        terminal <identifier>
{cstring}
        terminal <cstring>
{number}
        terminal <number>
[ \t]+
        skip
[\n\r]
        skip
.
        skip
%}

[ Description Text='Lexer for anglr file' Hover='true' ]
[
    UseScanner
        ScannerId='comment_ctx'
        ScannerId='attribute_ctx'
        ScannerId='scanner_id_ctx'
        ScannerId='scanner_part_ctx'
        ScannerId='regex_id_ctx'
        ScannerId='regex_part_ctx'
        ScannerId='regex_block_ctx'
        ScannerId='regex_block_part_ctx'
        InitialScanner='anglrScanner'
        Hover='true'
]
[ CompilationInfo ClassName='AnglrLexer' NameSpace='Anglr.Lexer' Access='public' Hover='true' ]
%lexer anglrLexer
%{
%}

[ Description Text='Parser for anglr file' Hover='true' ]
[ Lexer Id='anglrLexer' Hover='true' ]
[ Declarations Id='anglrDecls' Hover='true' ]
[ CompilationInfo ClassName='AnglrParser' NameSpace='Anglr.Parser' Access='public' Hover='true' ]
%parser anglrParser
%{

[ Start ]
<anglr source>
    : <attribute list terminal>
        ( : <attribute list> :

            ( : <attribute> :
                <left square bracket> <identifier>
                ( : <name value list optional> :
                    %empty
                |
                    ( : <name value list> :

                        ( : <name value pair> :
                            <identifier> <equals sign> <cstring>
                        )
                    | <name value list> <name value pair>
                    )
                )<right square bracket>
            )
        | <attribute list> <attribute>
        )
    | <attribute terminal> <attribute>
    | <name value list terminal> <name value list>
    | <name value pair terminal> <name value pair>
    | <anglr file terminal>
        ( : <anglr file> :
            %empty
        |
            ( : <anglr file part list> :

                ( : <anglr file part> :

                    ( : <general part> :

                        ( : <attribute list optional> :
                            %empty
                        | <attribute list>
                        )<general> <identifier> <left part bracket> <attribute list optional> <right part bracket>
                    )
                |
                    ( : <declaration part> :
                        <attribute list optional> <declarations> <identifier> <left part bracket>
                        ( : <anglr definition list optional> :
                            %empty
                        |
                            ( : <anglr definition list> :

                                ( : <anglr definition with attribute> :
                                    <attribute list optional>
                                    ( : <anglr definition> :

                                        ( : <single terminal definition> :
                                            <terminal>
                                            ( : <terminal definition> :
                                                <identifier>
                                                ( : <cstring optional> :
                                                    %empty
                                                | <cstring>
                                                )
                                            )
                                        )
                                    |
                                        ( : <single regex definition> :
                                            <regex>
                                            ( : <regex definition> :
                                                <identifier> <regular expression>
                                            )
                                        )
                                    |
                                        ( : <block of terminal definitions> :
                                            <terminal> <left curly bracket>
                                            ( : <block terminal definitions optional> :
                                                %empty
                                            |
                                                ( : <block terminal definitions> :

                                                    ( : <block terminal definition> :
                                                        <attribute list optional> <terminal definition>
                                                    )
                                                | <block terminal definitions> <block terminal definition>
                                                )
                                            )<right curly bracket>
                                        )
                                    |
                                        ( : <block of regex definitions> :
                                            <regex> <left curly bracket>
                                            ( : <block regex definitions optional> :
                                                %empty
                                            |
                                                ( : <block regex definitions> :

                                                    ( : <block regex definition> :
                                                        <attribute list optional> <regex definition>
                                                    )
                                                | <block regex definitions> <block regex definition>
                                                )
                                            )<right curly bracket>
                                        )
                                    )
                                )
                            | <anglr definition list> <anglr definition with attribute>
                            )
                        )<right part bracket>
                    )
                |
                    ( : <scanner part> :
                        <attribute list optional> <scanner> <identifier> <left part bracket>
                        ( : <regular expression list optional> :
                            %empty
                        |
                            ( : <regular expression list> :

                                ( : <regular expression usage> :
                                    <regular expression>
                                    ( : <actions optional> :
                                        %empty
                                    |
                                        ( : <actions> :

                                            ( : <action> :

                                                ( : <skip action> :
                                                    <skip>
                                                )
                                            |
                                                ( : <terminal action> :
                                                    <ttoken> <identifier>
                                                )
                                            |
                                                ( : <event action> :
                                                    <event> <identifier>
                                                )
                                            |
                                                ( : <push action> :
                                                    <push> <identifier>
                                                )
                                            |
                                                ( : <pop action> :
                                                    <pop>
                                                )
                                            )
                                        | <actions> <action>
                                        )
                                    )
                                )
                            | <regular expression list> <regular expression usage>
                            )
                        )<right part bracket>
                    )
                |
                    ( : <lexer part> :
                        <attribute list optional> <lexer> <identifier> <left part bracket> <attribute list optional> <right part bracket>
                    )
                |
                    ( : <parser part> :
                        <attribute list optional> <parser> <identifier> <left part bracket>
                        ( : <anglr syntax rule list optional> :
                            %empty
                        |
                            ( : <anglr syntax rule list> :

                                ( : <anglr syntax rule> :
                                    <attribute list optional> <identifier> <colon>
                                    ( : <anglr syntax production list> :

                                        ( : <anglr syntax production> :

                                            ( : <production name optional> :
                                                %empty
                                            |
                                                ( : <production name> :
                                                    <double at sign> <identifier>
                                                )
                                            )
                                            ( : <name list> :

                                                ( : <marker list optional> :
                                                    %empty
                                                |
                                                    ( : <marker list> :

                                                        ( : <marker> :
                                                            <at sign> <identifier>
                                                        )
                                                    | <marker list> <marker>
                                                    )
                                                )
                                            | <name list>
                                                ( : <g name> :

                                                    ( : <name> :
                                                        <any>
                                                    | <cstring>
                                                    | <identifier>
                                                    )
                                                | <left bracket>
                                                    ( : <anglr nested rule> :

                                                        ( : <anglr syntax production list name optional> :
                                                            %empty
                                                        |
                                                            ( : <anglr syntax production list name> :
                                                                <colon> <identifier> <colon>
                                                            )
                                                        )<anglr syntax production list>
                                                    )<right bracket>
                                                | <g name>
                                                    ( : <cardinality delimiter> :

                                                        ( : <cardinality> :
                                                            <question mark>
                                                        | <plus sign>
                                                        | <minus sign>
                                                        | <asterisk>
                                                        | <slash>
                                                        | <inv plus sign>
                                                        | <inv minus sign>
                                                        | <inv asterisk>
                                                        | <inv slash>
                                                        | <left curly bracket>
                                                            ( : <number optional> :
                                                                %empty
                                                            | <number>
                                                            )<comma> <number optional> <right curly bracket>
                                                        )
                                                        ( : <delimiter optional> :
                                                            %empty
                                                        |
                                                            ( : <delimiter> :
                                                                <left square bracket> <anglr nested rule> <right square bracket>
                                                            )
                                                        )
                                                    )
                                                )<marker list optional>
                                            )
                                        | <empty>
                                        )
                                    | <anglr syntax production list> <vertical bar> <anglr syntax production>
                                    )<semicolon>
                                | <attribute list optional> <identifier> <left curly bracket> <anglr syntax rule list optional> <right curly bracket>
                                )
                            | <anglr syntax rule list> <anglr syntax rule>
                            )
                        )<right part bracket>
                    )
                )
            | <anglr file part list> <anglr file part>
            )
        )
    | <anglr file part list terminal> <anglr file part list>
    | <anglr file part terminal> <anglr file part>
    | <general part terminal> <general part>
    | <declaration part terminal> <declaration part>
    | <anglr definition list terminal> <anglr definition list>
    | <anglr definition with attribute list terminal> <anglr definition with attribute>
    | <anglr definition terminal> <anglr definition>
    | <single terminal definition terminal> <single terminal definition>
    | <single regex definition terminal> <single regex definition>
    | <block of terminal definitions terminal> <block of terminal definitions>
    | <block of regex definitions terminal> <block of regex definitions>
    | <terminal definition terminal> <terminal definition>
    | <regex definition terminal> <regex definition>
    | <block terminal definitions terminal> <block terminal definitions>
    | <block terminal definition terminal> <block terminal definition>
    | <block regex definitions terminal> <block regex definitions>
    | <block regex definition terminal> <block regex definition>
    | <scanner part terminal> <scanner part>
    | <regular expression list terminal> <regular expression list>
    | <regular expression usage terminal> <regular expression usage>
    | <actions terminal> <actions>
    | <action terminal> <action>
    | <skip action terminal> <skip action>
    | <terminal action terminal> <terminal action>
    | <event action terminal> <event action>
    | <push action terminal> <push action>
    | <pop action terminal> <pop action>
    | <lexer part terminal> <lexer part>
    | <parser part terminal> <parser part>
    | <anglr syntax rule list terminal> <anglr syntax rule list>
    | <anglr syntax rule terminal> <anglr syntax rule>
    | <anglr nested rule terminal> <anglr nested rule>
    | <anglr syntax production list name terminal> <anglr syntax production list name>
    | <anglr syntax production list terminal> <anglr syntax production list>
    | <anglr syntax production terminal> <anglr syntax production>
    | <production name terminal> <production name>
    | <name list terminal> <name list>
    | <marker list terminal> <marker list>
    | <marker terminal> <marker>
    | <g name terminal> <g name>
    | <name terminal> <name>
    | <cardinality delimiter terminal> <cardinality delimiter>
    | <cardinality terminal> <cardinality>
    | <delimiter terminal> <delimiter>
    ;

%}