Jump to content

Translational Backus–Naur form

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Paulbmann (talk | contribs) at 19:52, 25 August 2018 (Brought the grammar up to date -- the latest version of TBNF (simpler that the original).). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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:

/* TBNF Grammar Sample. */
/* Input Tokens. */

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

/* Operator precedence. */

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

/* Productions. */

   Goal     -> Program... <eof>				*> goal_    (0,,"\t\tSTART\n"     ,,"\t\tEOF\n\n")          
             
   Program  -> 'program' <identifier> '{' Stmt... '}'	*> program_ (2,,"\t\tPROGRAM %s\n",,"\t\tEND PROGRAM %s\n")
				
   Stmt	    -> Assignment
 	    -> IfThen
    	    -> IfElse
            -> IfThenElse
		
   Assignment	~> Target '=' Exp ';'		    *> assign_  (0,,         ,,"\t\tSTORE\n")         
   IfThen	-> 'if' RelExp Then 'endif'	    *> if_	(0,,"if&0:\n",,"endif&0:\n" )
   IfElse	-> 'if' RelExp Else 'endif'	    *> if_      (0,,"if&0:\n",,"endif&0:\n" )
   IfThenElse	-> 'if' RelExp Then2 Else2 'endif'  *> if_      (0,,"if&0:\n",,"endif&0:\n" )
              
   Target   -> <identifier>			    *> ident_   (1,,,,"\t\tLADR %s\n")
             
   RelExp   -> Exp '==' Exp			    *> eq_	(0,,,,"\t\tEQ\n" ) 
            -> Exp '!=' Exp			    *> ne_	(0,,,,"\t\tNE\n" ) 
                                                        
   Exp      -> Primary          
            -> Exp '+' Exp			    *> add_     (0,,,,"\t\tADD\n") 
            -> Exp '-' Exp			    *> sub_	(0,,,,"\t\tSUB\n") 
            -> Exp '*' Exp			    *> mul_	(0,,,,"\t\tMUL\n") 
            -> Exp '/' Exp			    *> div_	(0,,,,"\t\tDIV\n") 
             
   Primary  -> <integer>			    *> intr_    (1,,,,"\t\tLOAD %s\n")
            -> <identifier>			    *> ident_   (1,,,,"\t\tLOAD %s\n")
            -> '(' Exp ')'  
             
   Then     -> 'then' Stmt...			    *> then_    (0,,"\t\tBR NZ endif&1\nthen&1:\n",,)
   Else     -> 'else' Stmt...			    *> else_    (0,,"\t\tBR Z endif&1\nelse&1:\n" ,,)
   Then2    -> 'then' Stmt...			    *> then2_   (0,,"\t\tBR NZ else&1\nthen&1:\n" ,,)
   Else2    -> 'else' Stmt...			    *> else2_   (0,,"\t\tBR endif&1\nelse&1:\n"   ,,)

References

  • LRSTAR an LR(*) parser generator that reads TBNF grammar notation and generates a program in C++

that translates source code to intermediate code.