BNF Grammar: Difference between revisions

Content added Content deleted
Line 4,623: Line 4,623:
=={{header|Visual Basic}}==
=={{header|Visual Basic}}==
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
<pre>
! -----------------------------------------------------------------------
! Visual Basic .NET
!
! Visual Basic .NET is the latest version in the long evoluation of the
! BASIC programming language. The Visual Basic .NET programming language
! is far more "clean" and orthagonal than its predecessors. Although some
! of the old constructs exist, the language is far easier to read and write.
!
! Major syntax changes include, but are not limited to:
!
! 1. The primative file access of VB6 was replaced by a class library. As
! a result, special statements such as
!
! Open "test" For Input As #1
!
! no longer exist.
!
! 2. Class module files were replaced by 'Class ... End Class' declarations
!
! 3. Module files were replaced by 'Module ... End Module' declarations
!
! 4. Structured error handling was added with C++ style Try ... Catch
! statements. The old nonstructured approach is still, unfortunately,
! available.
!
! Unfortnately, the designers of Visual Basic .NET did not remove the
! datatype postfix characters on identifiers. In the original BASIC,
! variables types were determined by $ for strings and % for integers.
! QuickBasic expanded the postix notation to include other symbols
! for long integers, singles, etc...
!
! Part of the ID terminal definition was commented out to prevent the
! old format. You can allow the postfix characters if you like.
!
! This grammar also does not contain the compiler directives.
!
! Note: This is an ad hoc version of the language. If there are any flaws,
! please visit www.DevinCook.com/GOLDParser
!
! Updates:
! 04/082005
! Devin Cook
! 1. Removed minus sign from the IntLiteral and RealLiteral
! definitions. These can cause some parse errors when expressions
! like "2-2" are read. In this case it would have been interpreted
! as "2" and "-2" rather than "2", "-" and "2".
! 2. Members of an enumeration can be defined with any expression.
! 3. Made some very minor comment changes - mainly for readability.
!
! 03/27/2005
! Adrian Moore [adrianrob@hotmail.com]
! 1. Add support for Implements in Class
! 2. Add support for AddressOf keyword
! 3. No longer fails if variable starts with _
!
! 02/24/2004
! Vladimir Morozov [vmoroz@hotmail.com] fixed a few flaws in the
! grammar. 1. The definition for strings did not allow the double
! double-quote override. 2. A real literal does not need to have a
! fraction if followed by an exponent. 3. Escaped identifiers can
! contain a null string and 4. Rem works the same as the '
!
!
! USE GOLD PARSER BUILDER VERSION 2.1 AND LATER TO COMPILE THIS GRAMMAR.
! Earlier versions cannot handle the complexity.
! -----------------------------------------------------------------------

"Name" = 'Visual Basic .NET'
"Author" = 'John G. Kemeny and Thomas E. Kurtz'
"Version" = '.NET'
"About" = 'Visual Basic .NET is the latest version in the long evoluation of the'
| 'BASIC programming language.'

"Case Sensitive" = False
"Start Symbol" = <Program>

! ----------------------------------------------------------------- Sets

{String Chars} = {Printable} - ["]
{Date Chars} = {Printable} - [#]
{ID Name Chars} = {Printable} - ['['']']
{Hex Digit} = {Digit} + [abcdef]
{Oct Digit} = [01234567]

{WS} = {Whitespace} - {CR} - {LF}
{Id Tail} = {Alphanumeric} + [_]

! ----------------------------------------------------------------- Terminals

NewLine = {CR}{LF} | {CR} | ':'
Whitespace = {WS}+ | '_' {WS}* {CR} {LF}?

Comment Line = '' | Rem !Fixed by Vladimir Morozov

LABEL = {Letter}{ID Tail}*':'

!Fixed by Vladimir Morozov

ID = [_]?{Letter}{ID Tail}* ! [%&@!#$]? !Archaic postfix chars
| '[' {ID Name Chars}* ']'
QualifiedID = ({Letter}{ID Tail}* | '['{ID Name Chars}*']') ( '.'({Letter}{ID Tail}* | '['{ID Name Chars}*']') )+

MemberID = '.' {Letter}{ID Tail}*
| '.[' {ID Name Chars}* ']'
!Fixed by Vladimir Morozov
StringLiteral = '"' ( {String Chars} | '""' )* '"'


CharLiteral = '"' {String Chars}* '"C'
IntLiteral = {digit}+ [FRDSIL]?

RealLiteral = {digit}* '.' {digit}+ ( 'E' [+-]? {Digit}+ )? [FR]?
| {digit}+ 'E' [+-]? {Digit}+ [FR]?


DateLiteral = '#'{Date chars}'#'

HexLiteral = '&H'{Hex Digit}+ [SIL]?
OctLiteral = '&O'{Oct Digit}+ [SIL]?

! ----------------------------------------------------------------- Rules

<Program> ::= <NameSpace Item> <Program>
| <Imports> <Program>
| <Option Decl> <Program>
|

! -------------------------------------------------------------------
! (Shared attributes)
! -------------------------------------------------------------------

<NL> ::= NewLine <NL>
| NewLine

<Modifiers> ::= <Modifier> <Modifiers>
|

<Modifier> ::= Shadows
| Shared
| MustInherit
| NotInheritable

| Overridable
| NotOverridable
| MustOverride
| Overrides
| Overloads
| Default
| ReadOnly
| WriteOnly
| <Access>

<Access Opt> ::= <Access>
|

<Access> ::= Public
| Private
| Friend
| Protected

<Var Member> ::= <Attributes> <Access> <Var Decl> <NL> !Variables
| <Attributes> <Access Opt> Const <Var Decl> <NL> !Constants
| <Attributes> <Access Opt> Static <Var Decl> <NL>
<Implements> ::= Implements <ID List>

<ID List> ::= <Identifier> ',' <ID List>
| <Identifier>
<Option Decl> ::= Option <IDs> <NL>

<IDs> ::= ID <IDs>
| ID
<Type> ::= As <Attributes> <Identifier>
|

<Compare Op> ::= '=' | '<>' | '<' | '>' | '>=' | '<='

! -------------------------------------------------------------------
! NameSpace
! -------------------------------------------------------------------

<NameSpace> ::= NameSpace ID <NL> <NameSpace Items> End NameSpace <NL>

<NameSpace Items> ::= <NameSpace Item> <NameSpace Items>
|

<NameSpace Item> ::= <Class>
| <Declare>
| <Delegate>
| <Enumeration>
| <Interface>
| <Structure>
| <Module>
| <Namespace>

! -------------------------------------------------------------------
! Attributes
! -------------------------------------------------------------------

<Attributes> ::= '<' <Attribute List> '>'
|

<Attribute List> ::= <Attribute> ',' <Attribute List>
| <Attribute>
<Attribute> ::= <Attribute Mod> ID <Argument List Opt>
<Attribute Mod> ::= Assembly
| Module
|
! -------------------------------------------------------------------
! Delegates
! -------------------------------------------------------------------
<Delegate> ::= <Attributes> <Modifiers> Delegate <Method>
| <Attributes> <Modifiers> Delegate <Declare>

! -------------------------------------------------------------------
! Imports
! -------------------------------------------------------------------

<Imports> ::= Imports <Identifier> <NL>
| Imports ID '=' <Identifier> <NL>

! -------------------------------------------------------------------
! Events
! -------------------------------------------------------------------

<Event Member> ::= <Attributes> <Modifiers> Event ID <Parameters Or Type> <Implements Opt> <NL>

<Parameters Or Type> ::= <Param List>
| As <Identifier>

<Implements Opt> ::= <Implements>
|
! -------------------------------------------------------------------
! Class
! -------------------------------------------------------------------

<Class> ::= <Attributes> <Modifiers> Class ID <NL> <Class Items> End Class <NL>


<Class Items> ::= <Class Item> <Class Items>
|

<Class Item> ::= <Declare>
| <Method>
| <Property>
| <Var Member>
| <Enumeration>
| <Inherits>
| <Class Implements>
<Inherits> ::= Inherits <Identifier> <NL>
<Class Implements> ::= Implements <ID List> <NL>

! -------------------------------------------------------------------
! Structures
! -------------------------------------------------------------------

<Structure> ::= <Attributes> <Modifiers> Structure ID <NL> <Structure List> End Structure <NL>

<Structure List> ::= <Structure Item> <Structure List>
|

<Structure Item> ::= <Implements>
| <Enumeration>
| <Structure>
| <Class>
| <Delegate>
| <Var Member>
| <Event Member>
| <Declare>
| <Method>
| <Property>

! -------------------------------------------------------------------
! Module
! -------------------------------------------------------------------

<Module> ::= <Attributes> <Modifiers> Module ID <NL> <Module Items> End Module <NL>

<Module Items> ::= <Module Item> <Module Items>
|
<Module Item> ::= <Declare>
| <Method>
| <Property>
| <Var Member>
| <Enumeration>
| <Option Decl>

! -------------------------------------------------------------------
! Interface
! -------------------------------------------------------------------

<Interface> ::= <Attributes> <Modifiers> Interface ID <NL> <Interface Items> End Interface <NL>


<Interface Items> ::= <Interface Item> <Interface Items>
|
<Interface Item> ::= <Implements>
| <Event Member>
| <Enum Member>
| <Method Member>
| <Property Member>

<Enum Member> ::= <Attributes> <Modifiers> Enum ID <NL>

<Method Member> ::= <Attributes> <Modifiers> Sub <Sub ID> <Param List> <Handles Or Implements> <NL>
| <Attributes> <Modifiers> Function ID <Param List> <Type> <Handles Or Implements> <NL>

<Property Member> ::= <Attributes> <Modifiers> Property ID <Param List> <Type> <Handles Or Implements> <NL>
! -------------------------------------------------------------------
! Parameters
! -------------------------------------------------------------------

<Param List Opt> ::= <Param List>
|

<Param List> ::= '(' <Param Items> ')'
| '(' ')'
<Param Items> ::= <Param Item> ',' <Param Items>
| <Param Item>

<Param Item> ::= <Param Passing> ID <Type>


<Param Passing> ::= ByVal
| ByRef
| Optional
| ParamArray
|

! -------------------------------------------------------------------
! Arguments
! -------------------------------------------------------------------

<Argument List Opt> ::= <Argument List>
|
<Argument List> ::= '(' <Argument Items> ')'
<Argument Items> ::= <Argument> ',' <Argument Items>
| <Argument>

<Argument> ::= <Expression>
| Id ':=' <Expression>
| !NULL
! -------------------------------------------------------------------
! Declares (External Procedures)
! -------------------------------------------------------------------

<Declare> ::= <Attributes> <Modifiers> Declare <Charset> Sub ID Lib StringLiteral <Alias> <Param List Opt> <NL>
| <Attributes> <Modifiers> Declare <Charset> Function ID Lib StringLiteral <Alias> <Param List Opt> <Type> <NL>

<Charset> ::= Ansi | Unicode | Auto | !Null

<Alias> ::= Alias StringLiteral
|


! -------------------------------------------------------------------
! Methods
! -------------------------------------------------------------------

<Method> ::= <Attributes> <Modifiers> Sub <Sub ID> <Param List> <Handles Or Implements> <NL> <Statements> End Sub <NL>
| <Attributes> <Modifiers> Function ID <Param List> <Type> <Handles Or Implements> <NL> <Statements> End Function <NL>
<Sub ID> ::= ID
| New !Class creation

<Handles Or Implements> ::= <Implements>
| <Handles>
|

<Handles> ::= Handles <ID List>

! -------------------------------------------------------------------
! Properties
! -------------------------------------------------------------------
<Property> ::= <Attributes> <Modifiers> Property ID <Param List> <Type> <NL> <Property Items> End Property <NL>

<Property Items> ::= <Property Item> <Property Items>
|

<Property Item> ::= Get <NL> <Statements> End Get <NL>
| Set <Param List> <NL> <Statements> End Set <NL>


! -------------------------------------------------------------------
! Enumerations
! -------------------------------------------------------------------

<Enumeration> ::= <Attributes> <Modifiers> Enum ID <NL> <Enum List> End Enum <NL>

<Enum List> ::= <Enum Item> <Enum List>
|

<Enum Item> ::= Id '=' <Expression> <NL>
| Id <NL>

! -------------------------------------------------------------------
! Variable Declaration
! -------------------------------------------------------------------

<Var Decl> ::= <Var Decl Item> ',' <Var Decl>
| <Var Decl Item>
<Var Decl Item> ::= <Var Decl ID> As <Identifier> <Argument List Opt>
| <Var Decl ID> As <Identifier> '=' <Expression> !Initialize
| <Var Decl ID> As New <Identifier> <Argument List Opt>
| <Var Decl ID>
| <Var Decl ID> '=' <Expression> !Initialize

<Var Decl ID> ::= ID <Argument List Opt>
! -------------------------------------------------------------------
! Normal Statements
! -------------------------------------------------------------------

<Statements> ::= <Statement> <Statements>
|

<Statement> ::= <Loop Stm>
| <For Stm>
| <If Stm>
| <Select Stm>
| <SyncLock Stm>
| <Try Stm>
| <With Stm>
| <Option Decl>
| <Local Decl>
| <Non-Block Stm> <NL> !Note the <NL>. A non-block statement can be a full statement
| LABEL <NL>
<Non-Block Stm> ::= Call <Variable>
| ReDim <Var Decl>
| ReDim Preserve <Var Decl>
| Erase ID
| Throw <Value>
| RaiseEvent <Identifier> <Argument List Opt>
| AddHandler <Expression> ',' <Expression>
| RemoveHandler <Expression> ',' <Expression>
| Exit Do
| Exit For
| Exit Function
| Exit Property
| Exit Select
| Exit Sub
| Exit Try
| Exit While
| GoTo ID !Argh - they still have this
| Return <Value>

| Error <Value> !Raise an error by number
| On Error GoTo IntLiteral ! 0 This is obsolete.
| On Error GoTo '-' IntLiteral !-1 This is obsolete.
| On Error GoTo Id
| On Error Resume Next
| Resume ID
| Resume Next
| <Variable> <Assign Op> <Expression>
| <Variable>
| <Method Call>

<Assign Op> ::= '=' | '^=' | '*=' | '/=' | '\=' | '+=' | '-=' | '&=' | '<<=' | '>>='


! -------------------------------------------------------------------
! Local declarations
! -------------------------------------------------------------------

<Local Decl> ::= Dim <Var Decl> <NL>
| Const <Var Decl> <NL>
| Static <Var Decl> <NL>

! -------------------------------------------------------------------
! Do Statement
! -------------------------------------------------------------------

<Loop Stm> ::= Do <Test Type> <Expression> <NL> <Statements> Loop <NL>
| Do <NL> <Statements> Loop <Test Type> <Expression> <NL>
| While <Expression> <NL> <Statements> End While <NL>

<Test Type> ::= While
| Until

! -------------------------------------------------------------------
! For Statement
! -------------------------------------------------------------------

<For Stm> ::= For <Identifier> '=' <Expression> To <Expression> <Step Opt> <NL> <Statements> Next <NL>
| For Each <Variable> In <Variable> <NL> <Statements> Next <NL>

<Step Opt> ::= Step <Expression>
|


! -------------------------------------------------------------------
! If Statement
! -------------------------------------------------------------------

<If Stm> ::= If <Expression> <Then Opt> <NL> <Statements> <If Blocks> End If <NL>
| If <Expression> Then <Non-Block Stm> <NL>
| If <Expression> Then <Non-Block Stm> Else <Non-Block Stm> <NL>

<Then Opt> ::= Then !!The reserved word 'Then' is optional for Block-If statements
|

<If Blocks> ::= ElseIf <Expression> <Then Opt> <NL> <Statements> <If Blocks>
| Else <NL> <Statements>
|

! -------------------------------------------------------------------
! Select Statement
! -------------------------------------------------------------------

<Select Stm> ::= Select <Case Opt> <Expression> <NL> <Select Blocks> End Select <NL>

<Case Opt> ::= Case !!The "Case" after Select is optional in VB.NEt
|


<Select Blocks> ::= Case <Case Clauses> <NL> <Statements> <Select Blocks>
| Case Else <NL> <Statements>
|

<Case Clauses> ::= <Case Clause> ',' <Case Clauses>
| <Case Clause>

<Case Clause> ::= <Is Opt> <Compare Op> <Expression>
| <Expression>
| <Expression> To <Expression>

<Is Opt> ::= Is
| !Null

! -------------------------------------------------------------------
! SyncLock Statement
! -------------------------------------------------------------------

<SyncLock Stm> ::= SyncLock <NL> <Statements> End SyncLock <NL>

! -------------------------------------------------------------------
! Try Statement
! -------------------------------------------------------------------

<Try Stm> ::= Try <NL> <Statements> <Catch Blocks> End Try <NL>

<Catch Blocks> ::= <Catch Block> <Catch Blocks>
| <Catch Block>

<Catch Block> ::= Catch <Identifier> As ID <NL> <Statements>
| Catch <NL> <Statements>

! -------------------------------------------------------------------
! With Statement
! -------------------------------------------------------------------

<With Stm> ::= With <Value> <NL> <Statements> End With <NL>
! -------------------------------------------------------------------
! Expressions
! -------------------------------------------------------------------

<Expression> ::= <And Exp> Or <Expression>
| <And Exp> OrElse <Expression>
| <And Exp> XOr <Expression>
| <And Exp>

<And Exp> ::= <Not Exp> And <And Exp>
| <Not Exp> AndAlso <And Exp>
| <Not Exp>
<Not Exp> ::= NOT <Compare Exp>
| <Compare Exp>

<Compare Exp> ::= <Shift Exp> <Compare Op> <Compare Exp> !e.g. x < y
| TypeOf <Add Exp> Is <Object>
| <Shift Exp> Is <Object>
| <Shift Exp> Like <Value>
| <Shift Exp>

<Shift Exp> ::= <Concat Exp> '<<' <Shift Exp>
| <Concat Exp> '>>' <Shift Exp>
| <Concat Exp>

<Concat Exp> ::= <Add Exp> '&' <Concat Exp>
| <Add Exp>

<Add Exp> ::= <Modulus Exp> '+' <Add Exp>
| <Modulus Exp> '-' <Add Exp>
| <Modulus Exp>

<Modulus Exp> ::= <Int Div Exp> Mod <Modulus Exp>
| <Int Div Exp>

<Int Div Exp> ::= <Mult Exp> '\' <Int Div Exp>
| <Mult Exp>

<Mult Exp> ::= <Negate Exp> '*' <Mult Exp>
| <Negate Exp> '/' <Mult Exp>
| <Negate Exp>

<Negate Exp> ::= '-' <Power Exp>
| <Power Exp>

<Power Exp> ::= <Power Exp> '^' <Value>
| <Value>

<Value> ::= '(' <Expression> ')'
| New <Identifier> <Argument List Opt>
| IntLiteral
| HexLiteral
| OctLiteral
| StringLiteral
| CharLiteral
| RealLiteral
| DateLiteral
| True
| False
| Me
| MyClass
| MyBase
| Nothing
| <Variable>
| AddressOf <Identifier>

<Object> ::= <Identifier> !Object identifiers
| Me
| MyClass
| MyBase
| Nothing

<Variable> ::= <Identifier> <Argument List Opt> <Method Calls>
<Method Calls> ::= <Method Call> <Method Calls>
|

<Method Call> ::= MemberID <Argument List Opt>


<Identifier> ::= ID | QualifiedID !Any type of identifier
</pre>

=={{header|Visual Objects}}==
=={{header|Visual Objects}}==
=={{header|Wrapl}}==
=={{header|Wrapl}}==