Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammar railroad diagram #1

Open
mingodad opened this issue Jul 12, 2021 · 0 comments
Open

Grammar railroad diagram #1

mingodad opened this issue Jul 12, 2021 · 0 comments

Comments

@mingodad
Copy link

Using this tool https://www.bottlecaps.de/convert/ and manually adding the tokens from p4/p4lexer.ll we can see a railroad diagram for the grammar in p4/p4parser.ypp copy and paste the EBNF shown bellow on https://www.bottlecaps.de/rr/ui in the tab Edit Grammar then switching to the tab View Diagram.

/*
From https://raw.githubusercontent.com/p4lang/p4c/530d3f0afd8cd6773646fc0294668cacefa98042/frontends/parsers/p4/p4parser.ypp
*/

/* converted on Mon Jul 12, 2021, 11:43 (UTC+02) by bison-to-w3c v0.52 which is Copyright (c) 2011-2021 by Gunther Rademacher <[email protected]> */

start    ::= fragment END_ANNOTATION
           | START_PROGRAM program
fragment ::= START_EXPRESSION_LIST expressionList
           | START_KV_LIST kvList
           | ( START_INTEGER_LIST ( INTEGER ',' )* | START_INTEGER | ( START_INTEGER_PAIR | START_INTEGER_TRIPLE INTEGER ',' ) INTEGER ',' ) INTEGER
           | ( START_INTEGER_OR_STRING_LITERAL_LIST ( intOrStr ',' )* | START_INTEGER_OR_STRING_LITERAL ) intOrStr
           | ( START_STRING_LITERAL_LIST ( STRING_LITERAL ',' )* | START_STRING_LITERAL | ( START_STRING_LITERAL_PAIR | START_STRING_LITERAL_TRIPLE STRING_LITERAL ',' ) STRING_LITERAL ',' ) STRING_LITERAL
           | ( START_EXPRESSION | ( START_EXPRESSION_PAIR | START_EXPRESSION_TRIPLE expression ',' ) expression ',' ) expression
           | START_P4RT_TRANSLATION_ANNOTATION STRING_LITERAL ',' p4rtControllerType
p4rtControllerType
         ::= STRING
           | BIT l_angle INTEGER r_angle
           | INTEGER
program  ::= ( declaration | ';' )* END
declaration
         ::= constantDeclaration
           | externDeclaration
           | actionDeclaration
           | parserDeclaration
           | typeDeclaration
           | controlDeclaration
           | instantiation
           | errorDeclaration
           | matchKindDeclaration
           | functionDeclaration
nonTypeName
         ::= IDENTIFIER
           | APPLY
           | KEY
           | ACTIONS
           | STATE
           | ENTRIES
           | TYPE
name     ::= nonTypeName
           | TYPE_IDENTIFIER
nonTableKwName
         ::= IDENTIFIER
           | TYPE_IDENTIFIER
           | APPLY
           | STATE
           | TYPE
optCONST ::= CONST?
optAnnotations
         ::= annotations?
annotations
         ::= annotation+
annotation
         ::= '@' name ( '(' annotationBody ')' | '[' ( expressionList | kvList ) ']' )?
           | PRAGMA name annotationBody END_PRAGMA
annotationBody
         ::= ( annotationBody ( '(' annotationBody ')' | annotationToken ) )?
annotationToken
         ::= UNEXPECTED_TOKEN
           | ABSTRACT
           | ACTION
           | ACTIONS
           | APPLY
           | BOOL
           | BIT
           | CONST
           | CONTROL
           | DEFAULT
           | ELSE
           | ENTRIES
           | ENUM
           | ERROR
           | EXIT
           | EXTERN
           | FALSE
           | HEADER
           | HEADER_UNION
           | IF
           | IN
           | INOUT
           | INT
           | KEY
           | MATCH_KIND
           | TYPE
           | OUT
           | PARSER
           | PACKAGE
           | PRAGMA
           | RETURN
           | SELECT
           | STATE
           | STRING
           | STRUCT
           | SWITCH
           | TABLE
           | THIS
           | TRANSITION
           | TRUE
           | TUPLE
           | TYPEDEF
           | VARBIT
           | VALUESET
           | VOID
           | '_'
           | IDENTIFIER
           | TYPE_IDENTIFIER
           | STRING_LITERAL
           | INTEGER
           | '&&&'
           | '..'
           | '<<'
           | '&&'
           | '||'
           | '=='
           | '!='
           | '>='
           | '<='
           | '++'
           | '+'
           | '|+|'
           | '-'
           | '|-|'
           | '*'
           | '/'
           | '%'
           | '|'
           | '&'
           | '^'
           | '~'
           | '['
           | ']'
           | '{'
           | '}'
           | '<'
           | L_ANGLE_ARGS
           | '>'
           | R_ANGLE_SHIFT
           | '!'
           | ':'
           | ','
           | '?'
           | '.'
           | '='
           | ';'
           | '@'
kvList   ::= kvPair ( ',' kvPair )*
kvPair   ::= name '=' expression
parameterList
         ::= ( parameter ( ',' parameter )* )?
parameter
         ::= optAnnotations direction typeRef name ( '=' expression )?
direction
         ::= ( IN | OUT | INOUT )?
packageTypeDeclaration
         ::= optAnnotations PACKAGE name optTypeParameters '(' parameterList ')'
instantiation
         ::= annotations? typeRef '(' argumentList ')' name ( '=' objInitializer )? ';'
objInitializer
         ::= '{' objDeclaration* '}'
objDeclaration
         ::= functionDeclaration
           | instantiation
optConstructorParameters
         ::= ( '(' parameterList ')' )?
parserDeclaration
         ::= parserTypeDeclaration optConstructorParameters '{' parserLocalElement* parserState+ '}'
parserLocalElement
         ::= constantDeclaration
           | instantiation
           | variableDeclaration
           | valueSetDeclaration
parserTypeDeclaration
         ::= optAnnotations PARSER name optTypeParameters '(' parameterList ')'
parserState
         ::= optAnnotations STATE name '{' parserStatements transitionStatement '}'
parserStatements
         ::= parserStatement*
parserStatement
         ::= assignmentOrMethodCallStatement
           | directApplication
           | ';'
           | variableDeclaration
           | constantDeclaration
           | parserBlockStatement
           | conditionalStatement
parserBlockStatement
         ::= optAnnotations '{' parserStatements '}'
transitionStatement
         ::= ( TRANSITION stateExpression )?
stateExpression
         ::= name ';'
           | selectExpression
selectExpression
         ::= SELECT '(' expressionList ')' '{' selectCase* '}'
selectCase
         ::= keysetExpression ':' name ';'
keysetExpression
         ::= tupleKeysetExpression
           | simpleKeysetExpression
tupleKeysetExpression
         ::= '(' ( simpleKeysetExpression ( ',' simpleKeysetExpression )+ | reducedSimpleKeysetExpression ) ')'
reducedSimpleKeysetExpression
         ::= expression ( '&&&' | '..' ) expression
           | DEFAULT
           | '_'
simpleKeysetExpression
         ::= expression ( ( '&&&' | '..' ) expression )?
           | DEFAULT
           | '_'
valueSetDeclaration
         ::= optAnnotations VALUESET l_angle ( baseType | tupleType | typeName ) r_angle '(' expression ')' name ';'
controlDeclaration
         ::= controlTypeDeclaration optConstructorParameters '{' controlLocalDeclaration* APPLY controlBody '}'
controlTypeDeclaration
         ::= optAnnotations CONTROL name optTypeParameters '(' parameterList ')'
controlLocalDeclaration
         ::= constantDeclaration
           | actionDeclaration
           | tableDeclaration
           | instantiation
           | variableDeclaration
controlBody
         ::= blockStatement
externDeclaration
         ::= optAnnotations EXTERN ( nonTypeName optTypeParameters '{' methodPrototype* '}' | ( functionPrototype | name ) ';' )
functionPrototype
         ::= typeOrVoid name optTypeParameters '(' parameterList ')'
methodPrototype
         ::= optAnnotations ( ABSTRACT? functionPrototype | TYPE_IDENTIFIER '(' parameterList ')' ) ';'
typeRef  ::= baseType
           | typeName
           | specializedType
           | headerStackType
           | tupleType
namedType
         ::= typeName
           | specializedType
prefixedType
         ::= '.'? TYPE_IDENTIFIER
typeName ::= prefixedType
tupleType
         ::= TUPLE l_angle typeArgumentList r_angle
headerStackType
         ::= ( typeName | specializedType ) '[' expression ']'
specializedType
         ::= typeName l_angle typeArgumentList r_angle
baseType ::= BOOL
           | ERROR
           | ( BIT | INT ) ( l_angle ( INTEGER | '(' expression ')' ) r_angle )?
           | STRING
           | VARBIT l_angle ( INTEGER | '(' expression ')' ) r_angle
typeOrVoid
         ::= typeRef
           | VOID
           | IDENTIFIER
optTypeParameters
         ::= typeParameters?
typeParameters
         ::= l_angle name ( ',' name )* r_angle
typeArg  ::= typeRef
           | nonTypeName
           | VOID
           | '_'
typeArgumentList
         ::= typeArg? ( ',' typeArg )*
realTypeArg
         ::= typeRef
           | VOID
           | '_'
realTypeArgumentList
         ::= realTypeArg ( ',' typeArg )*
typeDeclaration
         ::= derivedTypeDeclaration
           | ( typedefDeclaration | parserTypeDeclaration | controlTypeDeclaration | packageTypeDeclaration ) ';'
derivedTypeDeclaration
         ::= headerTypeDeclaration
           | headerUnionDeclaration
           | structTypeDeclaration
           | enumDeclaration
headerTypeDeclaration
         ::= optAnnotations HEADER name optTypeParameters '{' structFieldList '}'
structTypeDeclaration
         ::= optAnnotations STRUCT name optTypeParameters '{' structFieldList '}'
headerUnionDeclaration
         ::= optAnnotations HEADER_UNION name optTypeParameters '{' structFieldList '}'
structFieldList
         ::= structField*
structField
         ::= optAnnotations typeRef name ';'
enumDeclaration
         ::= optAnnotations ENUM ( name '{' identifierList | typeRef name '{' specifiedIdentifier ( ',' specifiedIdentifier )* ) '}'
specifiedIdentifier
         ::= name '=' initializer
errorDeclaration
         ::= ERROR '{' identifierList '}'
matchKindDeclaration
         ::= MATCH_KIND '{' identifierList '}'
identifierList
         ::= name ( ',' name )*
typedefDeclaration
         ::= optAnnotations ( TYPEDEF | TYPE ) ( typeRef | derivedTypeDeclaration ) name
assignmentOrMethodCallStatement
         ::= lvalue ( ( l_angle typeArgumentList r_angle )? '(' argumentList ')' | '=' expression ) ';'
exitStatement
         ::= EXIT ';'
returnStatement
         ::= RETURN expression? ';'
conditionalStatement
         ::= IF '(' expression ')' statement ( ELSE statement )?
directApplication
         ::= typeName '.' APPLY '(' argumentList ')' ';'
statement
         ::= assignmentOrMethodCallStatement
           | directApplication
           | conditionalStatement
           | ';'
           | blockStatement
           | returnStatement
           | exitStatement
           | switchStatement
blockStatement
         ::= optAnnotations '{' statementOrDeclaration* '}'
switchStatement
         ::= SWITCH '(' expression ')' '{' switchCase* '}'
switchCase
         ::= switchLabel ':' blockStatement?
switchLabel
         ::= DEFAULT
           | ( INTEGER | STRING_LITERAL | TRUE | FALSE | THIS | '.'? nonTypeName | '(' ( expression ')' | typeRef ')' expression ) | ( '!' | '~' | '-' | '+' ) expression | typeName dot_name | ERROR '.' name | namedType '(' argumentList ')' ) ( '[' expression ( ':' expression )? ']' | dot_name | ( '*' | '/' | '%' | '+' | '-' | '|+|' | '|-|' | '<<' | R_ANGLE_SHIFT? '>' | '<=' | '>=' | '!=' | '==' | '&' | '^' | '|' | '++' | '&&' | '||' | '?' expression ':' ) expression | l_angle ( expression | realTypeArgumentList r_angle '(' argumentList ')' ) | '(' argumentList ')' )*
statementOrDeclaration
         ::= variableDeclaration
           | constantDeclaration
           | statement
           | instantiation
tableDeclaration
         ::= optAnnotations TABLE name '{' tableProperty+ '}'
tableProperty
         ::= ( KEY '=' '{' keyElement* | ACTIONS '=' '{' ( optAnnotations actionRef ';' )* ) '}'
           | optAnnotations optCONST ( ENTRIES '=' '{' entry+ '}' | nonTableKwName '=' initializer ';' )
keyElement
         ::= expression ':' name optAnnotations ';'
actionRef
         ::= prefixedNonTypeName ( '(' argumentList ')' )?
entry    ::= keysetExpression ':' actionRef optAnnotations ';'
actionDeclaration
         ::= optAnnotations ACTION name '(' parameterList ')' blockStatement
variableDeclaration
         ::= annotations? typeRef name optInitializer ';'
constantDeclaration
         ::= optAnnotations CONST typeRef name '=' initializer ';'
optInitializer
         ::= ( '=' initializer )?
initializer
         ::= expression
functionDeclaration
         ::= functionPrototype blockStatement
argumentList
         ::= ( argument ( ',' argument )* )?
argument ::= ( name '=' )? expression
           | '_'
expressionList
         ::= expression? ( ',' expression )*
prefixedNonTypeName
         ::= '.'? nonTypeName
dot_name ::= '.' name
lvalue   ::= ( prefixedNonTypeName | THIS ) ( dot_name | '[' expression ( ':' expression )? ']' )*
expression
         ::= INTEGER
           | STRING_LITERAL
           | TRUE
           | FALSE
           | THIS
           | '.'? nonTypeName
           | expression ( '[' expression ( ':' expression )? ']' | dot_name | ( '*' | '/' | '%' | '+' | '-' | '|+|' | '|-|' | '<<' | R_ANGLE_SHIFT? '>' | '<=' | '>=' | '!=' | '==' | '&' | '^' | '|' | '++' | '&&' | '||' | '?' expression ':' ) expression | l_angle ( expression | realTypeArgumentList r_angle '(' argumentList ')' ) | '(' argumentList ')' )
           | '{' ( expressionList | kvList ) '}'
           | '(' ( expression ')' | typeRef ')' expression )
           | ( '!' | '~' | '-' | '+' ) expression
           | typeName dot_name
           | ERROR '.' name
           | namedType '(' argumentList ')'
intOrStr ::= INTEGER
           | STRING_LITERAL
l_angle  ::= '<'
           | L_ANGLE_ARGS
r_angle  ::= '>'
           | R_ANGLE_SHIFT

// Tokens from https://github.com/p4lang/p4c/blob/530d3f0afd8cd6773646fc0294668cacefa98042/frontends/parsers/p4/p4lexer.ll

PRAGMA ::= "@pragma"
ABSTRACT ::= "abstract"
ACTION ::= "action"
ACTIONS ::= "actions"
APPLY ::= "apply"
BOOL ::= "bool"
BIT ::= "bit"
CONST ::= "const"
CONTROL ::= "control"
DEFAULT ::= "default"
ELSE ::= "else"
ENTRIES ::= "entries"
ENUM ::= "enum"
ERROR ::= "error"
EXIT ::= "exit"
EXTERN ::= "extern"
FALSE ::= "false"
HEADER ::= "header"
HEADER_UNION ::= "header_union"
IF ::= "if"
IN ::= "in"
INOUT ::= "inout"
INT ::= "int"
KEY ::= "key"
MATCH_KIND ::= "match_kind"
TYPE ::= "type"
OUT ::= "out"
PARSER ::= "parser"
PACKAGE ::= "package"
RETURN ::= "return"
SELECT ::= "select"
STATE ::= "state"
STRING ::= "string"
STRUCT ::= "struct"
SWITCH ::= "switch"
TABLE ::= "table"
THIS ::= "this"
TRANSITION ::= "transition"
TRUE ::= "true"
TUPLE ::= "tuple"
TYPEDEF ::= "typedef"
VARBIT ::= "varbit"
VALUESET ::= "value_set"
VOID ::= "void"
DONTCARE ::= "_"

MASK ::= "&&&"
RANGE ::= ".."
SHL ::= "<<"
AND ::= "&&"
OR ::= "||"
EQ ::= "=="
NE ::= "!="
GE ::= ">="
LE ::= "<="
PP ::= "++"
PLUS ::= "+"
PLUS_SAT ::= "|+|"
MINUS ::= "-"
MINUS_SAT ::= "|-|"
MUL ::= "*"
DIV ::= "/"
MOD ::= "%"
BIT_OR ::= "|"
BIT_AND ::= "&"
BIT_XOR ::= "^"
COMPLEMENT ::= "~"
L_PAREN ::= "("
R_PAREN ::= ")"
L_BRACKET ::= "["
R_BRACKET ::= "]"
L_BRACE ::= "{"
R_BRACE ::= "}"
L_ANGLE ::= "<"
R_ANGLE_SHIFT ::= ">"
R_ANGLE ::= ">"
NOT ::= "!"
COLON ::= ":"
COMMA ::= ","
QUESTION ::= "?"
DOT ::= "."
ASSIGN ::= "="
SEMICOLON ::= ";"
AT ::= "@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant