Translational Backus–Naur form

Translational Backus–Naur form (TBNF, or translational BNF) refers to Backus–Naur form, which is a formal grammar notation used to define the syntax of computer languages, such as Algol, Ada, C++, COBOL, Fortran, Java, Perl, Python, and many others. TBNF goes beyond BNF and extended BNF (EBNF) grammar notation because it not only defines the syntax of a language, but also defines the structure of the abstract syntax tree (AST) to be created in memory and the output intermediate code to be generated. Thus TBNF defines the complete translation process from input text to intermediate code. Specification of the output intermediate code is optional, in which case you will still get automatic AST creation and have the ability to define its structure in the grammar.

The TBNF concept was first published in April 2006 in a paper at SIGPLAN Notices, a special interest group of the ACM.[1]

Here is a sample grammar specified in TBNF:

/* Sample Grammar. */

/* Input Tokens. */

   <identifier>	=> lookup();   // Lookup & store in symbol table.
   <integer>	=> lookup();   // Lookup & store in symbol table. 

/* Operator precedence. */

   { '==' '!=' }  <<  // Lowest priority.    
   { '+'  '-'  }  <<  
   { '*'  '/'  }  <<  // Highest priority.

/* Productions. */

   Goal     -> Program... <eof>			*> goal_()  emit ("\t\tSTART\n"     ,,"\t\tEOF\n")          
             
   Program  -> 'program' <identifier> '{' Stmt... '}'	*> program_(2) emit ("\t\tPROGRAM %s\n",,"\t\tEND PROGRAM %s\n")
				
   Stmt	    -> Assignment
            -> IfThen
	    -> IfElse
	    -> IfThenElse
		
   Assignment  ~> Target '=' Exp ';'               *> assign_() emit (          ,,"\t\tSTORE\n")         
   IfThen      -> 'if' RelExp Then 'endif'         *> if_()     emit ("if&0:\n" ,,"endif&0:\n" )
   IfElse      -> 'if' RelExp Else 'endif'         *> if_()     emit ("if&0:\n" ,,"endif&0:\n" )
   IfThenElse  -> 'if' RelExp Then2 Else2 'endif'  *> if_()     emit ("if&0:\n" ,,"endif&0:\n" )
              
   Target      -> <identifier>                     *> target_(1) emit (          ,,"\t\tLADR %s\n")
             
   RelExp   -> Exp '==' Exp                        *> eq_(2) emit (          ,,"\t\tEQ\n" ) 
            -> Exp '!=' Exp                        *> ne_(2) emit (          ,,"\t\tNE\n" ) 

   Exp      -> Primary
            -> Exp '+' Exp                         *> add_(2) emit (          ,,"\t\tADD\n") 
            -> Exp '-' Exp                         *> sub_(2) emit (          ,,"\t\tSUB\n") 
            -> Exp '*' Exp                         *> mul_(2) emit (          ,,"\t\tMUL\n") 
            -> Exp '/' Exp                         *> div_(2) emit (          ,,"\t\tDIV\n") 
             
   Primary  -> <identifier>                        *> id_(1)  emit (          ,,"\t\tLOAD %s\n")
            -> <integer>                           *> int_(1) emit (          ,,"\t\tLOAD %s\n")
            -> '(' Exp ')'  
             
   Then     -> 'then' Stmt...                      *> then_() emit ("\t\tBR NZ endif&1\nthen&1:\n",,)
   Else     -> 'else' Stmt...                      *> else_() emit ("\t\tBR Z endif&1\nelse&1:\n" ,,)
   Then2    -> 'then' Stmt...                      *> then_() emit ("\t\tBR NZ else&1\nthen&1:\n" ,,)
   Else2    -> 'else' Stmt...                      *> else_() emit ("\t\tBR endif&1\nelse&1:\n"   ,,)

/* End of Grammar. */

References

External links

This article is issued from Wikipedia - version of the 10/28/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.