Skip to content

Refine parserStatement grammar - #1415

Open
jaehyun1ee wants to merge 1 commit into
p4lang:mainfrom
jaehyun1ee:parser-conditional-stmt
Open

Refine parserStatement grammar#1415
jaehyun1ee wants to merge 1 commit into
p4lang:mainfrom
jaehyun1ee:parser-conditional-stmt

Conversation

@jaehyun1ee

@jaehyun1ee jaehyun1ee commented May 27, 2026

Copy link
Copy Markdown
Contributor

This is a follow-up from #1375.

Previously, parserStatment grammar was defined as:

parserStatement
    : assignmentOrMethodCallStatement
    | directApplication
    | emptyStatement
    | variableDeclaration
    | constantDeclaration
    | parserBlockStatement
    | conditionalStatement
    ;

conditionalStatement
    : IF "(" expression ")" statement
    | IF "(" expression ")" statement ELSE statement
    ;

where the conditionalStatement actually led to ordinary statements (which allow more production than parserStatements) being syntactically included within parserStatement.

This PR amends the parserStatement grammar such that it is closed under itself, without a path to statement. In short, the grammar is refined as follows:

 // tag::parserState[]
 parserState
     : optAnnotations STATE name
-      "{" parserStatements transitionStatement "}"
+      "{" parserStatOrDeclList transitionStatement "}"
     ;
 // end::parserState[]

-// tag::parserStatements[]
-parserStatements
+// tag::parserStatementOrDeclaration[]
+parserStatementOrDeclaration
+    : variableDeclaration
+    | constantDeclaration
+    | parserStatement
+    ;
+// end::parserStatementOrDeclaration[]
+
+// tag::parserStatOrDeclList[]
+parserStatOrDeclList
     : /* empty */
-    | parserStatements parserStatement
+    | parserStatOrDeclList parserStatementOrDeclaration
     ;
-// end::parserStatements[]
+// end::parserStatOrDeclList[]

 // tag::parserStatement[]
 parserStatement
     : assignmentOrMethodCallStatement
     | directApplication
     | emptyStatement
-    | variableDeclaration
-    | constantDeclaration
     | parserBlockStatement
-    | conditionalStatement
+    | parserConditionalStatement
     ;
 // end::parserStatement[]

 // tag::parserBlockStatement[]
 parserBlockStatement
-    : optAnnotations "{" parserStatements "}"
+    : optAnnotations "{" parserStatOrDeclList "}"
     ;
 // end::parserBlockStatement[]

+// tag::parserConditionalStatement[]
+parserConditionalStatement
+    : IF "(" expression ")" parserStatement
+    | IF "(" expression ")" parserStatement ELSE parserStatement
+    ;
+// end::parserConditionalStatement[]
+

Similar to how statement is defined,

  • this adds parserStatementOrDeclaration, parserStatOrDeclList to the grammar, where parserBlockStatement and parserState has parserStatOrDeclList as its field, and
  • parserStatement has a new parserConditionalStatement variant with explicit then- and else- branches that are parserStatements.

A mirror of this PR in p4c is also made: p4lang/p4c#5638. I've checked with p4-16/spec/scriptscompare-grammar.sh that the grammar consistent with the p4c PR.

Signed-off-by: jaehyun1ee <99jaehyunlee@kaist.ac.kr>

@jafingerhut jafingerhut left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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

Successfully merging this pull request may close these issues.

2 participants