Anonymous user
BNF Grammar: Difference between revisions
m
→Pascal: move to pacsal page
m (→Pascal: move to pacsal page) |
|||
(79 intermediate revisions by 25 users not shown) | |||
Line 1:
{{DeprecatedTask}}
In computer science, Backus–Naur Form (BNF) is a metasyntax used to express context-free grammars: that is, a formal way to describe formal languages. John Backus and Peter Naur developed a context free grammar to define the syntax of a programming language by using two sets of rules: i.e., lexical rules and syntactic rules.
Line 6:
There are many extensions and variants of BNF, including Extended and Augmented Backus–Naur Forms (EBNF and ABNF).
'''This is a deprecated task. Please move these grammars to the language's category page, or somewhere else.'''
==[[:Category:BASIC|BASIC]]==
<div style="height:30ex;overflow:scroll"><pre>
! -----------------------------------------------------------------------
! BASIC '64
Line 688 ⟶ 173:
| String
| Real
</pre></div>
==[[:Category:BASIC Commodore PET|BASIC Commodore PET]]==
<div style="height:30ex;overflow:scroll"><pre>
! -----------------------------------------------------------------------
! Commodore PET BASIC
Line 893 ⟶ 379:
| Real
</pre></div>
==[[:Category:Brainf***|Brainf***]]==
Code ::= Command Code | <NONE>
Command ::= "+" | "-" | "<" | ">" | "," | "." | "[" Code "]" | <ANY>
==[[:Category:PARI/GP|PARI/GP]]==
[http://pari.math.u-bordeaux.fr/cgi-bin/viewcvs.cgi/trunk/src/language/parse.y?view=markup&revision=12950&root=pari parse.y] contains a grammar for GP. The grammar for PARI is that of [http://c.comsci.us/syntax/ C].
==[[:Category:PowerShell|PowerShell]]==
An annotated version of the PowerShell grammar can be found in Bruce Payette's book ''Windows PowerShell in Action''. The appendix containing the grammar is available in [http://www.manning.com/payette/AppCexcerpt.pdf PDF form] on the publisher's site.
This grammar does not accurately represent the PowerShell language, though, as for example the <code>for</code> loop mandates semicolons in the grammar but in practice does not require them when arguments are omitted. The infinite loop may be represented by
<lang powershell>for () {}</lang>
but the grammar would require
<lang powershell>for (;;) {}</lang>
==[[:Category:VBScript|VBScript]]==
<div style="height:30ex;overflow:scroll"><pre>
!===============================
! VB Script grammar.
!
! To create the grammar I was using Microsoft's VB Script documentation
! available from http://msdn.microsoft.com/scripting,
! VB Script parser from ArrowHead project http://www.tripi.com/arrowhead/,
! and Visual Basic .Net grammar file written by Devin Cook.
!
! This grammar cannot cover all aspects of VBScript and may have some errors.
! Feel free to contact me if you find any flaws in the grammar.
!
! Vladimir Morozov vmoroz@hotmail.com
!
! Special thanks to Nathan Baulch for the grammar updates.
!
! USE GOLD PARSER BUILDER VERSION 2.1 AND LATER TO COMPILE THIS GRAMMAR.
!===============================
"Name" = '
"Author" = 'John G. Kemeny and Thomas E. Kurtz'
"
"About" = 'VB Script grammar.'
"Case Sensitive" = False
"Start Symbol" = <Program>
!===============================
! Character sets
!===============================
{String Char} = {All Valid} - ["]
{Date Char} = {Printable} - [#]
{ID Name Char} = {Printable} - ['['']']
{Hex Digit} = {Digit} + [abcdef]
{Oct Digit} = [01234567]
{WS} = {Whitespace} - {CR} - {LF}
{ID Tail} = {Alphanumeric} + [_]
!===============================
! Terminals
!===============================
! Special white space definition. Whitespace is either space or tab, which
! can be followed by continuation symbol '_' followed by new line character
Whitespace = {WS}+
| '_' {WS}* {CR}? {LF}?
! Special comment definition
Comment Line = ''
| 'Rem'
! Literals
StringLiteral = '"' ( {String Char} | '""' )* '"'
IntLiteral = {Digit}+
HexLiteral = '&H' {Hex Digit}+ '&'?
OctLiteral = '&' {Oct Digit}+ '&'?
FloatLiteral = {Digit}* '.' {Digit}+ ( 'E' [+-]? {Digit}+ )?
| {Digit}+ 'E' [+-]? {Digit}+
DateLiteral = '#' {Date Char}+ '#'
! Identifier is either starts with letter and followed by letter,
! number or underscore, or it can be escaped sequence of any printable
! characters ([] and [_$% :-) @] are valid identifiers)
ID = {Letter} {ID Tail}*
| '[' {ID Name Char}* ']'
! White space is not allowed to be before dot, but allowed to be after it.
| '[' {ID Name Char}* ']' '.'
| 'And.'
| 'ByRef.'
| 'ByVal.'
| 'Call.'
| 'Case.'
| 'Class.'
| 'Const.'
| 'Default.'
| 'Dim.'
| 'Do.'
| 'Each.'
| 'Else.'
| 'ElseIf.'
| 'Empty.'
| 'End.'
| 'Eqv.'
| 'Erase.'
| 'Error.'
| 'Exit.'
| 'Explicit.'
| 'False.'
| 'For.'
| 'Function.'
| 'Get.'
| 'GoTo.'
| 'If.'
| 'Imp.'
| 'In.'
| 'Is.'
| 'Let.'
| 'Loop.'
| 'Mod.'
| 'New.'
| 'Next.'
| 'Not.'
| 'Nothing.'
| 'Null.'
| 'On.'
| 'Option.'
| 'Or.'
| 'Preserve.'
| 'Private.'
| 'Property.'
| 'Public.'
| 'Redim.'
| 'Rem.'
| 'Resume.'
| 'Select.'
| 'Set.'
| 'Step.'
| 'Sub.'
| 'Then.'
| 'To.'
| 'True.'
| 'Until.'
| 'WEnd.'
| 'While.'
| 'With.'
| 'Xor.'
! The following identifiers should only be used in With statement.
! This rule must be checked by contextual analyzer.
DotID = '.' {Letter} {ID Tail}*
| '.' '[' {ID Name Char}* ']'
| '.And'
| '.ByRef'
| '.ByVal'
| '.Call'
| '.Case'
| '.Class'
| '.Const'
| '.Default'
| '.Dim'
| '.Do'
| '.Each'
| '.Else'
| '.ElseIf'
| '.Empty'
| '.End'
| '.Eqv'
| '.Erase'
| '.Error'
| '.Exit'
| '.Explicit'
| '.False'
| '.For'
| '.Function'
| '.Get'
| '.GoTo'
| '.If'
| '.Imp'
| '.In'
| '.Is'
| '.Let'
| '.Loop'
| '.Mod'
| '.New'
| '.Next'
| '.Not'
| '.Nothing'
| '.Null'
| '.On'
| '.Option'
| '.Or'
| '.Preserve'
| '.Private'
| '.Property'
| '.Public'
| '.Redim'
| '.Rem'
| '.Resume'
| '.Select'
| '.Set'
| '.Step'
| '.Sub'
| '.Then'
| '.To'
| '.True'
| '.Until'
| '.WEnd'
| '.While'
| '.With'
| '.Xor'
DotIDDot = '.' {Letter}{ID Tail}* '.'
| '.' '[' {ID Name Char}* ']' '.'
| '.And.'
| '.ByRef.'
| '.ByVal.'
| '.Call.'
| '.Case.'
| '.Class.'
| '.Const.'
| '.Default.'
| '.Dim.'
| '.Do.'
| '.Each.'
| '.Else.'
| '.ElseIf.'
| '.Empty.'
| '.End.'
| '.Eqv.'
| '.Erase.'
| '.Error.'
| '.Exit.'
| '.Explicit.'
| '.False.'
| '.For.'
| '.Function.'
| '.Get.'
| '.GoTo.'
| '.If.'
| '.Imp.'
| '.In.'
| '.Is.'
| '.Let.'
| '.Loop.'
| '.Mod.'
| '.New.'
| '.Next.'
| '.Not.'
| '.Nothing.'
| '.Null.'
| '.On.'
| '.Option.'
| '.Or.'
| '.Preserve.'
| '.Private.'
| '.Property.'
| '.Public.'
| '.Redim.'
| '.Rem.'
| '.Resume.'
| '.Select.'
| '.Set.'
| '.Step.'
| '.Sub.'
| '.Then.'
| '.To.'
| '.True.'
| '.Until.'
| '.WEnd.'
| '.While.'
| '.With.'
| '.Xor.'
!===============================
! Rules
!===============================
<NL> ::= NewLine <NL>
| NewLine
<Program> ::= <NLOpt> <GlobalStmtList>
!===============================
! Rules : Declarations
!===============================
<ClassDecl> ::= 'Class' <ExtendedID> <NL> <MemberDeclList> 'End' 'Class' <NL>
<MemberDeclList> ::= <MemberDecl> <MemberDeclList>
<MemberDecl> ::= <FieldDecl>
| <VarDecl>
| <ConstDecl>
| <SubDecl>
| <FunctionDecl>
| <PropertyDecl>
<FieldDecl> ::= 'Private' <FieldName> <OtherVarsOpt> <NL>
| 'Public' <FieldName> <OtherVarsOpt> <NL>
<FieldName> ::= <FieldID> '(' <ArrayRankList> ')'
| <FieldID>
<FieldID> ::= ID
| 'Default'
| 'Erase'
| 'Error'
| 'Explicit'
| 'Step'
<VarDecl> ::= 'Dim' <VarName> <OtherVarsOpt> <NL>
<VarName> ::=
| <ExtendedID>
<OtherVarsOpt> ::= ',' <VarName> <OtherVarsOpt>
|
<ArrayRankList> ::= <IntLiteral> ',' <ArrayRankList>
| <IntLiteral>
|
<ConstDecl> ::= <AccessModifierOpt> 'Const' <ConstList> <NL>
<ConstList> ::= <ExtendedID> '=' <ConstExprDef> ',' <ConstList>
| <ExtendedID> '=' <ConstExprDef>
<ConstExprDef> ::= '(' <ConstExprDef> ')'
| '-' <ConstExprDef>
| '+'
| <ConstExpr>
<SubDecl> ::= <MethodAccessOpt> 'Sub' <ExtendedID> <MethodArgList> <NL> <MethodStmtList> 'End' 'Sub' <NL>
| <MethodAccessOpt> 'Sub' <ExtendedID> <MethodArgList> <InlineStmt> 'End' 'Sub' <NL>
<FunctionDecl> ::= <MethodAccessOpt> 'Function' <ExtendedID> <MethodArgList> <NL> <MethodStmtList> 'End' 'Function' <NL>
| <MethodAccessOpt> 'Function' <ExtendedID> <MethodArgList> <InlineStmt> 'End' 'Function' <NL>
<MethodAccessOpt> ::= 'Public' 'Default'
| <AccessModifierOpt>
<AccessModifierOpt> ::= 'Public'
|
<MethodArgList> ::= '(' <ArgList> ')'
<ArgList> ::= <Arg> ',' <ArgList>
| <Arg>
<Arg> ::= <ArgModifierOpt> <ExtendedID> '(' ')'
| <ArgModifierOpt> <ExtendedID>
<ArgModifierOpt> ::= 'ByVal'
<PropertyDecl> ::= <MethodAccessOpt> 'Property' <PropertyAccessType> <ExtendedID> <MethodArgList> <NL> <MethodStmtList> 'End' 'Property' <NL>
<PropertyAccessType> ::= 'Get'
| 'Let'
| 'Set'
!===============================
! Rules : Statements
!===============================
<GlobalStmt> ::= <OptionExplicit>
| <ConstDecl>
| <SubDecl>
| <FunctionDecl>
| <BlockStmt>
<MethodStmt> ::= <ConstDecl>
<BlockStmt> ::= <VarDecl>
| <WithStmt>
| <SelectStmt>
| <LoopStmt>
| <ForStmt>
| <InlineStmt> <NL>
<InlineStmt> ::= <AssignStmt>
| <ErrorStmt>
| <ExitStmt>
| 'Erase' <ExtendedID>
<GlobalStmtList> ::= <GlobalStmt> <GlobalStmtList>
<MethodStmtList> ::= <MethodStmt> <MethodStmtList>
<BlockStmtList> ::= <BlockStmt> <BlockStmtList>
|
<OptionExplicit> ::= 'Option' 'Explicit' <NL>
<ErrorStmt> ::= 'On' 'Error' 'Resume' 'Next'
| 'On' 'Error' 'GoTo' IntLiteral ! must be 0
<ExitStmt> ::= 'Exit' 'Do'
| 'Exit' 'For'
| 'Exit' 'Function'
| 'Exit' 'Property'
| 'Exit' 'Sub'
<
| 'Set' <LeftExpr> '=' 'New' <LeftExpr>
! Hack: VB Script allows to have construct a = b = c, which means a = (b = c)
! In this grammar we do not allow it in order to prevent complications with
! interpretation of a(1) = 2, which may be considered as array element assignment
! or a subroutine call: a ((1) = 2).
! Note: VBScript allows to have missed parameters: a ,,2,3,
! VM: If somebody knows a better way to do it, please let me know
<SubCallStmt> ::= <QualifiedID> <SubSafeExprOpt> <CommaExprList>
| <QualifiedID> <SubSafeExprOpt>
| <QualifiedID> '(' <Expr> ')' <CommaExprList>
| <QualifiedID> '(' <Expr> ')'
| <QualifiedID> '(' ')'
| <QualifiedID> <IndexOrParamsList> '.' <LeftExprTail> <SubSafeExprOpt> <CommaExprList>
| <QualifiedID> <IndexOrParamsListDot> <LeftExprTail> <SubSafeExprOpt> <CommaExprList>
| <QualifiedID> <IndexOrParamsList> '.' <LeftExprTail> <SubSafeExprOpt>
| <QualifiedID> <IndexOrParamsListDot> <LeftExprTail> <SubSafeExprOpt>
! This very simplified case - the problem is that we cannot use parenthesis in aaa(bbb).ccc (ddd)
<SubSafeExprOpt> ::= <SubSafeExpr>
|
<CallStmt> ::= 'Call' <LeftExpr>
<LeftExpr> ::= <QualifiedID> <IndexOrParamsList> '.' <LeftExprTail>
| <QualifiedID> <IndexOrParamsListDot> <LeftExprTail>
| <QualifiedID> <IndexOrParamsList>
| <QualifiedID>
| <SafeKeywordID>
<LeftExprTail> ::= <QualifiedIDTail> <IndexOrParamsList> '.' <LeftExprTail>
| <QualifiedIDTail> <IndexOrParamsListDot> <LeftExprTail>
| <QualifiedIDTail> <IndexOrParamsList>
| <QualifiedIDTail>
! VB Script does not allow to have space between Identifier and dot:
! a . b - Error ; a. b or a.b - OK
<QualifiedID> ::= IDDot <QualifiedIDTail>
| DotIDDot <QualifiedIDTail>
| ID
| DotID
<QualifiedIDTail> ::= IDDot <QualifiedIDTail>
| ID
| <KeywordID>
<KeywordID> ::= <SafeKeywordID>
| 'And'
| 'ByRef'
| 'ByVal'
| 'Call'
| 'Case'
| 'Class'
| 'Const'
| 'Dim'
| 'Do'
| 'Each'
| 'Else'
| 'ElseIf'
| 'Empty'
| 'End'
| 'Eqv'
| 'Exit'
| 'False'
| 'For'
| 'Function'
| 'Get'
| 'GoTo'
| 'If'
| 'Imp'
| 'In'
| 'Is'
| 'Let'
| 'Loop'
| 'Mod'
| 'New'
| 'Next'
| 'Not'
| 'Nothing'
| 'Null'
| 'On'
| 'Option'
| 'Or'
| 'Preserve'
| 'Private'
| 'Public'
| 'Redim'
| 'Resume'
| 'Select'
| 'Set'
| 'Sub'
| 'Then'
| 'To'
| 'True'
| 'Until'
| 'WEnd'
| 'While'
| 'With'
| 'Xor'
<SafeKeywordID> ::= 'Default'
| 'Erase'
| 'Error'
| 'Explicit'
| 'Property'
| 'Step'
<ExtendedID> ::= <SafeKeywordID>
| ID
<IndexOrParamsList> ::= <IndexOrParams> <IndexOrParamsList>
| <IndexOrParams>
<IndexOrParams> ::= '(' <Expr> <CommaExprList> ')'
| '(' <CommaExprList> ')'
| '(' <Expr> ')'
| '(' ')'
<IndexOrParamsListDot> ::= <IndexOrParams> <IndexOrParamsListDot>
| <IndexOrParamsDot>
<IndexOrParamsDot> ::= '(' <Expr> <CommaExprList> ').'
| '(' <CommaExprList> ').'
| '(' <Expr> ').'
| '(' ').'
<CommaExprList> ::= ',' <Expr> <CommaExprList>
| ',' <CommaExprList>
| ',' <Expr>
| ','
!========= Redim Statement
<RedimStmt> ::= 'Redim' <RedimDeclList> <NL>
| 'Redim' 'Preserve' <RedimDeclList> <NL>
<RedimDeclList> ::= <RedimDecl> ',' <RedimDeclList>
| <RedimDecl>
<RedimDecl> ::= <ExtendedID> '(' <ExprList> ')'
<IfStmt> ::= 'If' <Expr> 'Then' <NL> <BlockStmtList> <ElseStmtList> 'End' 'If' <NL>
| 'If' <Expr> 'Then' <InlineStmt> <ElseOpt> <EndIfOpt> <NL>
<ElseStmtList> ::= 'ElseIf' <Expr> 'Then' <NL> <BlockStmtList> <ElseStmtList>
| 'ElseIf' <Expr> 'Then' <InlineStmt> <NL> <ElseStmtList>
| 'Else' <InlineStmt> <NL>
| 'Else' <NL> <BlockStmtList>
|
<ElseOpt> ::= 'Else' <InlineStmt>
|
<EndIfOpt> ::= 'End' 'If'
|
!========= With Statement
<WithStmt> ::= 'With' <Expr> <NL> <BlockStmtList> 'End' 'With' <NL>
!========= Loop Statement
<LoopStmt> ::= 'Do' <LoopType> <Expr> <NL> <BlockStmtList> 'Loop' <NL>
| 'Do' <NL> <BlockStmtList> 'Loop' <LoopType> <Expr> <NL>
| 'Do' <NL> <BlockStmtList> 'Loop' <NL>
| 'While' <Expr> <NL> <BlockStmtList> 'WEnd' <NL>
<LoopType> ::= 'While'
| 'Until'
!========= For Statement
<ForStmt> ::= 'For' <ExtendedID> '=' <Expr> 'To' <Expr> <StepOpt> <NL> <BlockStmtList> 'Next' <NL>
| 'For' 'Each' <ExtendedID> 'In' <Expr> <NL> <BlockStmtList> 'Next' <NL>
<StepOpt> ::= 'Step' <Expr>
|
!========= Select Statement
<SelectStmt> ::= 'Select' 'Case' <Expr> <NL> <CaseStmtList> 'End' 'Select' <NL>
<CaseStmtList> ::= 'Case' <ExprList> <NLOpt> <BlockStmtList> <CaseStmtList>
| 'Case' 'Else' <NLOpt> <BlockStmtList>
|
<NLOpt> ::= <NL>
|
<ExprList> ::= <Expr> ',' <ExprList>
| <Expr>
!===============================
! Rules : Expressions
!===============================
<SubSafeExpr> ::= <SubSafeImpExpr>
<SubSafeImpExpr> ::= <SubSafeImpExpr> 'Imp' <EqvExpr>
| <SubSafeEqvExpr>
<SubSafeEqvExpr> ::= <SubSafeEqvExpr> 'Eqv' <XorExpr>
| <SubSafeXorExpr>
<SubSafeXorExpr> ::= <SubSafeXorExpr> 'Xor' <OrExpr>
| <SubSafeOrExpr>
<SubSafeOrExpr> ::= <SubSafeOrExpr> 'Or' <AndExpr>
| <SubSafeAndExpr>
<SubSafeAndExpr> ::= <SubSafeAndExpr> 'And' <NotExpr>
| <SubSafeNotExpr>
<SubSafeNotExpr> ::= 'Not' <NotExpr>
| <SubSafeCompareExpr>
<SubSafeCompareExpr> ::= <SubSafeCompareExpr> 'Is' <ConcatExpr>
| <SubSafeCompareExpr> 'Is' 'Not' <ConcatExpr>
| <SubSafeCompareExpr> '>=' <ConcatExpr>
| <SubSafeCompareExpr> '=>' <ConcatExpr>
| <SubSafeCompareExpr> '<=' <ConcatExpr>
| <SubSafeCompareExpr> '=<' <ConcatExpr>
| <SubSafeCompareExpr> '>' <ConcatExpr>
| <SubSafeCompareExpr> '<' <ConcatExpr>
| <SubSafeCompareExpr> '<>' <ConcatExpr>
| <SubSafeCompareExpr> '=' <ConcatExpr>
| <SubSafeConcatExpr>
<SubSafeConcatExpr> ::= <SubSafeConcatExpr> '&' <AddExpr>
| <SubSafeAddExpr>
<SubSafeAddExpr> ::= <SubSafeAddExpr> '+' <ModExpr>
| <SubSafeAddExpr> '-' <ModExpr>
| <SubSafeModExpr>
<SubSafeModExpr> ::= <SubSafeModExpr> 'Mod' <IntDivExpr>
| <SubSafeIntDivExpr>
<SubSafeIntDivExpr> ::= <SubSafeIntDivExpr> '\' <MultExpr>
| <SubSafeMultExpr>
<SubSafeMultExpr> ::= <SubSafeMultExpr> '*' <UnaryExpr>
| <SubSafeMultExpr> '/' <UnaryExpr>
| <SubSafeUnaryExpr>
<SubSafeUnaryExpr> ::= '-' <UnaryExpr>
| <SubSafeExpExpr>
<
| <SubSafeValue>
<SubSafeValue> ::= <ConstExpr>
| <LeftExpr>
! | '(' <Expr> ')'
<Expr> ::= <ImpExpr>
<ImpExpr> ::= <ImpExpr> 'Imp' <EqvExpr>
| <EqvExpr>
<EqvExpr> ::= <EqvExpr> 'Eqv' <XorExpr>
<
| <OrExpr>
<OrExpr> ::= <OrExpr> 'Or' <AndExpr>
<AndExpr> ::= <AndExpr> 'And' <NotExpr>
<NotExpr> ::= 'Not' <NotExpr>
<CompareExpr> ::= <CompareExpr> 'Is' <ConcatExpr>
| <
| <
| <
| <
| <
| <CompareExpr> '>' <ConcatExpr>
| <CompareExpr> '<' <ConcatExpr>
| <CompareExpr> '<>' <ConcatExpr>
| <CompareExpr> '=' <ConcatExpr>
| <ConcatExpr>
<
<AddExpr> ::= <AddExpr> '+' <ModExpr>
| <AddExpr> '-' <ModExpr>
| <ModExpr>
<ModExpr> ::= <ModExpr> 'Mod' <IntDivExpr>
| <IntDivExpr>
<IntDivExpr> ::= <IntDivExpr> '\' <MultExpr>
<MultExpr> ::= <MultExpr> '*' <UnaryExpr>
| <
| <
<UnaryExpr> ::= '-' <UnaryExpr>
| '
<ExpExpr> ::= <Value> '^' <ExpExpr>
<Value> ::= <ConstExpr>
|
| '(' <Expr> ')'
<
| FloatLiteral
| StringLiteral
| DateLiteral
| <Nothing>
<BoolLiteral> ::= 'True'
| 'False'
<IntLiteral> ::= IntLiteral
| OctLiteral
<Nothing> ::= 'Nothing'
| 'Null'
| 'Empty'
</pre></div>
==[[:Category:Visual Basic .NET|Visual Basic .NET]]==
The following link (Appedix B) has a simple BNF Syntax [http://laser.physics.sunysb.edu/~amol/papers/mollypaper.pdf VB Syntax]
<div style="height:30ex;overflow:scroll"><pre>
! -----------------------------------------------------------------------
! Visual Basic .NET
Line 7,804 ⟶ 1,811:
<Identifier> ::= ID | QualifiedID !Any type of identifier
</pre></div>
|