BNF Grammar: Difference between revisions

Content added Content deleted
Line 4,319: Line 4,319:


=={{header|Perl}}==
=={{header|Perl}}==
The BNF description follows, [http://perl.comsci.us/syntax/statement/index.html Perl Syntax]
<div style="height:30ex;overflow:scroll"><pre>
! -----------------------------------------------------------------------
! Perl.grm
!
!
! -----------------------------------------------------------------------

! -----------------------------------------------------------------------
! This grammar does not contain the compiler directives.
!
! Note: This is an ad hoc version of the language. If there are any flaws,
! please visit the contact page and tell me.
!
! SPECIAL THANKS TO:
! Scott Gever (who taught me most of what I know about Perl)
!
! Modified 04/28/2009
! -----------------------------------------------------------------------


"Name" = 'Perl'
"Version" = '5.0'
"Author" = 'Robin Randall (Larry Wall wrote Perl)'
"About" = 'Perl is one of the most common, and complex, programming languages in use today.'

"Case Sensitive" = True
"Start Symbol" = <Decls>

{Hex Digit} = {Digit} + [abcdefABCDEF]
{Oct Digit} = [01234567]

{Id Head} = {Letter} + [_]
{Id Tail} = {Id Head} + {Digit}

{String} = {Printable} - ["\]
!GIM = [g]?[i]?[m]?
DecLiteral = [123456789]{digit}*
OctLiteral = 0{Oct Digit}*
HexLiteral = 0x{Hex Digit}+
FloatLiteral = {Digit}*'.'{Digit}+

StringLiteral = '"'( {String} | '\'{Printable} )* '"'
!CharLiteral = '' ( {Char} | '\'{Printable} )''

Id = {Id Head}{Id Tail}*
{Spec} = [.?*+-_^$()#@%&,;:<>\|/{}~] + ['['] + [']'] + ['='] + [' ']
{Char} = {Printable} - {Spec}

! ===================================================================
! Comments
! ===================================================================

Comment Line = '#'

!
!
!
Whitespace = {Whitespace}+
! | '#'{Printable}*
!
!
! POD = '='{Printable}* '=' cut
!=======================================================
! Preparation
!=======================================================
<Decls> ::= <Decl> <Decls>
|

<Decl> ::= <Sub Decl>
| <Sub Proto>
| <Var Decl>
| <Prep Stm>

<Prep Stm> ::= use <Op Prep>
| requires <Module>
| enum <Module>

<Op Prep> ::= strict ';'
| warnings ';'
| constant Id '=>' <Expr> ';'
| <Module>

<Module> ::= Id '::' Id ';'
| Id ';'
! ===================================================================
! Function Declaration
! ===================================================================

<Sub Proto> ::= sub <Sub ID> '(' <Params> ')' ';'
|sub <Sub ID> '(' ')' ';'

<Sub Decl> ::= sub <Sub ID> '(' <Params> ')' <Block>
| sub <Sub ID> '(' <Id List> ')' <Block>
| sub <Sub ID> '(' ')' <Block>


<Params> ::= <Param> ',' <Params>
| <Param>
<Param> ::= const <Type> Id
| <Type> Id
| <Type>'_'
<Id List> ::= Id ',' <Id List>
| Id

<Sub ID> ::= '&'Id
| Id

! ===================================================================
! Type Declaration
! ===================================================================


! ===================================================================
! Variable Declaration
! ===================================================================

<Var Decl> ::= <Mod> <Var> ',' <Var List> ';'
| <Mod> <Var> ';'
| <Var> ';'

<Var> ::= <Type> '_' !Default
| <Type> '\' '=' <Op Assign>
| <Type> Id '=' <Op Assign>
| <Type> Id

<Var List> ::= <Var Item> ',' <Var List>
| <Var Item>

<Var Item> ::= <Scalar>
| <Array>
| <Hash>
| <Ref>
<Mod> ::= const
| undef
| my
| sub

! ===================================================================
! Types
! ===================================================================

<Type> ::= '$' !Scalar
| '@' !Array
| '%' !Hash
| '&' !Subroutine
| '\' !Reference
|

<Scalar> ::= '$' Id
| '$' Id '[' <Expr> ']'

<Array> ::= '@' Id
<Hash> ::= '%' Id
<Ref> ::= '\' <Var Item>

! ===================================================================
! Statements
! ===================================================================

<Stm> ::= <Var Decl>
| Id ':' ';'
| Id ':' <Comp Stm>
| <Comp Stm>
| Id ':' <Normal Stm> ';'
| <Normal Stm> ';'
| <Normal Stm> <Stm Mod> ';'

<Comp Stm> ::= if '(' <Expr> ')' <Block> else <Block>
| if '(' <Expr> ')' <Block> elsif '(' <Expr> ')' <Block>
| while '(' <Expr> ')' <Block>
| while '(' <Expr> ')' <Block> continue <Block>
| until '(' <Expr> ')' <Block>
| until '(' <Expr> ')' <Block> continue <Block>
| for '(' <Expr> ';' <Expr> ';' <Expr> ')' <Block>
| foreach <Value> '(' <List> ')' <Block>
| foreach <Value> '(' <List> ')' <Block> continue <Block>
| <Block> continue <Block>
| <Block>
| do <Block>

<Normal Stm> ::= <Expr>
! | goto Label
! | goto <Expr>
| break
| next
| last
| redo
| continue
| reset
| return <Expr>
| !Null statement


<Block> ::= '{' <Stm List> '}'

<Stm List> ::= <Stm> <Stm List>
| <Stm>

<Stm Mod> ::= if <Expr>
| unless <Expr>
| while <Expr>
| until <Expr>
| foreach <List>
<List> ::= <Value> ',' <List>
| <Value>'..'<Value>
|
! ===================================================================
! Here begins Perl's 15 levels of operator precedence.
! ===================================================================

<Expr> ::= <Expr> ',' <Op Assign>
| <Op Assign>
| '<'ID'>'


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

<Op If> ::= <Op Or> '?' <Op If> ':' <Op If>
| <Op Or>

<Op Or> ::= <Op Or> '||' <Op And>
| <Op And>

<Op And> ::= <Op And> '&&' <Op BinOR>
| <Op BinOR>

<Op BinOR> ::= <Op BinOr> '|' <Op BinXOR>
| <Op BinXOR>

<Op BinXOR> ::= <Op BinXOR> '^' <Op BinAND>
| <Op BinAND>

<Op BinAND> ::= <Op BinAND> '&' <Op Equate>
| <Op Equate>

<Op Equate> ::= <Op Equate> '==' <Op Compare>
| <Op Equate> '!=' <Op Compare>
| <Op Equate> '=~' 's/' <RE>'/' <Expr> '/' !GIM
| <Op Equate> '=~' 'm/' <RE>'/' !GIM
| <Op Equate> '=~' '/' <RE>'/' !GIM
| <Op Compare>



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

<Op Shift> ::= <Op Shift> '<<' <Op Add>
| <Op Shift> '>>' <Op Add>
| <Op Add>

<Op Add> ::= <Op Add> '+' <Op Mult>
| <Op Add> '-' <Op Mult>
| <Op Mult>

<Op Mult> ::= <Op Mult> '*' <Op Unary>
| <Op Mult> '/' <Op Unary>
| <Op Mult> '%' <Op Unary>
| <Op Unary>
| <Value>

<Op Unary> ::= '\!' <Op Unary>
| '~' <Op Unary>
| '-' <Op Unary>
| '*' <Op Unary>
| '&' <Op Unary>
| '++' <Op Unary>
| '--' <Op Unary>

<Value> ::= OctLiteral
| HexLiteral
| DecLiteral
| StringLiteral
| FloatLiteral
| <Type> Id '(' <Expr> ')'
| <Type> Id '(' ')'
| '(' <Expr> ')'
!====================================================
! Regular Expressions
!====================================================
<RE> ::= <union>
| <simple-RE>
<union> ::= <RE> '|' <simple-RE>
| '^' <simple-RE>
| <simple-RE> '$'
| '^' <simple-RE> '$'
<simple-RE> ::= <concat>
| <basic-RE>
<concat> ::= <simple-RE> <basic-RE>
<basic-RE> ::= <star>
| <plus>
| <elem-RE>
<star> ::= <elem-RE> '*'
<plus> ::= <elem-RE> '+'
<elem-RE> ::= <group>
| <any>
| <set>

<group> ::= '(' <RE> ')'
<any> ::= '.'
<set> ::= <pos-set>
| <neg-set>
<pos-set> ::= '[' <set-items> ']'
<neg-set> ::= '[^' <set-items> ']'
<set-items> ::= <set-item>
| <set-item> <set-items>
<set-item> ::= <range>
| char
<range> ::= char '-' char
char = {Char}
</pre></div>


=={{header|Python}}==
=={{header|Python}}==