Browsable PHP 3,4,5 Grammar

CC-BY

Grammar extracted by Vadim Zaytsev, see the Grammar Zoo entry for details: php/cordy/extracted
Source used for this grammar: James R. Cordy, TXL Grammar for PHP 3,4,5, Version 1.0, June 2007

Summary

Syntax

program ::=
	Document
Document ::=
	PHPOpenTag? PHPSection PHPCloseTagDocumentSection?
	HTMLSection PHPOpenTagPHPSection? PHPCloseTagDocumentSection?
PHPCloseTagDocumentSection ::=
	PHPCloseTag HTMLSection PHPCloseTagDocumentSection?
PHPOpenTagPHPSection ::=
	PHPOpenTag PHPSection
PHPSection ::=
	TopStatement*
PHPOpenTag ::=
	"<?php" NL
	"<?" NL
	"<%" NL
	"<?=" NL
PHPCloseTag ::=
	"?>" NL
	"%>" NL
HTMLSection ::=
	InlineHTML
InlineHTML ::=
	InlineHTMLtoken*
InlineHTMLtoken ::=
	tag_token_or_key
tag_token_or_key ::=
	tag
	";" NL
	token
	key
tag ::=
	"<" SPOFF bigtag_id tag_attribute* ">" SPON (IN NL)
	EX FL "<" SPOFF "/" bigtag_id ">" SPON FL
	"<" SPOFF "!"? id_or_key tag_attribute* ">" SPON NL
	"<" SPOFF "/" id_or_key ">" SPON NL
	"<" SPOFF id_or_key tag_attribute* "/>" SPON NL
bigtag_id ::=
	"html"
	"body"
	"head"
	"form"
	"table"
	"tr"
	"td"
	"script"
	"select"
	"label"
	"div"
tag_attribute ::=
	SP xml_colon? id_or_key "=" token
xml_colon ::=
	"xml:"
id_or_key ::=
	id "-"? id?
	key
TopStatement ::=
	FunctionDecl
	ClassDecl
	Statement
	InterfaceDecl
FunctionDecl ::=
	"function" "&"? id "(" {Param ","}* ")" NL Block
ClassDecl ::=
	ClassType id NL ExtendsClause? ImplementsClause? "{" (NL IN) ClassMember* EX "}" NL
ClassType ::=
	"class"
	"abstract" "class"
	"final" "class"
ExtendsClause ::=
	"extends" id NL
ImplementsClause ::=
	"implements" {Name ","}+ NL
Name ::=
	id
ClassMember ::=
	VarModifiers? FunctionDecl
	VarModifiers? "abstract" VarModifiers? "function" "&"? id "(" {Param ","}* ")" ";" NL
	"var" {InstanceVariable ","}+ ";" NL
	"const" id "=" StaticScalar ";" NL
	VarModifiers {InstanceVariable ","}+ ";" NL
InterfaceDecl ::=
	"interface" id NL InterfaceExtendsClause? "{" (NL IN) InterfaceMember* (EX NL) "}"
InterfaceExtendsClause ::=
	"extends" {Name ","}+
InterfaceMember ::=
	VarModifiers? "function" "&"? id "(" {Param ","}* ")" ";" NL
InstanceVariable ::=
	TVariable EqualsStaticScalar?
TVariable ::=
	SimpleVariableName
EqualsStaticScalar ::=
	"=" StaticScalar
VarModifiers ::=
	VarModifier+
VarModifier ::=
	"public"
	"protected"
	"private"
	"static"
	"final"
Param ::=
	TypeHint? "&"? TVariable EqualsStaticScalar?
	"const" TVariable
TypeHint ::=
	id
Statement ::=
	ExprStatement
	BlockStatement
	IfStatement
	WhileStatement
	DoStatement
	ForStatement
	SwitchStatement
	ControlStatement
	GlobalStaticStatement
	UnsetStatement
	EchoStatement
	PrintStatement
	InlineHTMLStatement
	ForEachStatement
	NullStatement
	DeclareStatement
	TryStatement
	ThrowStatement
ExprStatement ::=
	Expr ";"? NL
BlockStatement ::=
	Block
Block ::=
	"{" (NL IN) TopStatement* EX "}" NL
IfStatement ::=
	"if" "(" Expr ")" NL Statement ElseIfStatement* ElseStatement?
	"if" "(" Expr ")" ":" (NL IN) TopStatement* EX AltElseifStatement* AltElseStatement? "endif" ";" NL
ElseIfStatement ::=
	"elseif" "(" Expr ")" NL Statement
ElseStatement ::=
	"else" NL Statement
AltElseifStatement ::=
	"elseif" "(" Expr ")" ":" (NL IN) TopStatement* EX
AltElseStatement ::=
	"else" ":" (NL IN) TopStatement* EX
WhileStatement ::=
	"while" "(" Expr ")" NL Statement
	"while" "(" Expr ")" ":" (NL IN) TopStatement* EX "endwhile" ";" NL
DoStatement ::=
	"do" NL Statement "while" "(" Expr ")" ";" NL
ForStatement ::=
	"for" "(" {Expr ","}* ";" {Expr ","}* ";" {Expr ","}* ")" NL Statement
	"for" "(" {Expr ","}* ";" {Expr ","}* ";" {Expr ","}* ")" ":" (NL IN) Statement* EX "endfor" ";" NL
SwitchStatement ::=
	"switch" "(" Expr ")" NL "{" ";"? (NL IN) Case* EX "}" NL
	"switch" "(" Expr ")" ":" ";"? (NL IN) Case* EX "endswitch" ";" NL
Case ::=
	"case" Expr CaseSeparator (NL IN) TopStatement* EX
	"default" CaseSeparator (NL IN) TopStatement* EX
CaseSeparator ::=
	";"
	":"
ControlStatement ::=
	"break" Expr? ";" NL
	"continue" Expr? ";" NL
	"return" Expr? ";" NL
GlobalStaticStatement ::=
	"global" {CVar ","}+ ";" NL
	"static" {StaticVariable ","}+ ";" NL
StaticVariable ::=
	TVariable EqualsStaticScalar?
UnsetStatement ::=
	"unset" "(" {ObjectCVar ","}+ ")" ";" NL
EchoStatement ::=
	"echo" {Expr ","}+ ";"? NL
PrintStatement ::=
	"print" {Expr ","}+ ";"? NL
InlineHTMLStatement ::=
	PHPCloseTag InlineHTML PHPOpenTag
ForEachStatement ::=
	"foreach" "(" Expr "as" ForEachPattern ")" NL Statement
	"foreach" "(" Expr "as" ForEachPattern ")" ":" (NL IN) Statement* EX "endforeach" ";" NL
ForEachPattern ::=
	ForEachKey? ForEachVar
ForEachKey ::=
	ForEachVar "=>"
ForEachVar ::=
	CVar
	"&" CVar
NullStatement ::=
	";" NL
DeclareStatement ::=
	"declare" "(" Directive* ")" NL Statement
Directive ::=
	id
TryStatement ::=
	"try" NL Block Catch*
Catch ::=
	"catch" "(" id TVariable ")" NL Block
ThrowStatement ::=
	"throw" Expr ";" NL
Expr ::=
	LogicalOrExpr
LogicalOrExpr ::=
	LogicalXorExpr OrLogicalXorExpr*
OrLogicalXorExpr ::=
	"or" LogicalXorExpr
LogicalXorExpr ::=
	LogicalAndExpr LogicalXorLogicalAndExpr*
LogicalXorLogicalAndExpr ::=
	"xor" LogicalAndExpr
LogicalAndExpr ::=
	AssignmentExpr LogicalAndAssignmentExpr*
LogicalAndAssignmentExpr ::=
	"and" AssignmentExpr
AssignmentExpr ::=
	TernaryExpr AssignOpAssignmentExpr*
AssignOpAssignmentExpr ::=
	AssignOp AssignmentExpr
AssignOp ::=
	".="
	"="
	"+="
	"-="
	"*="
	"/="
	"%="
	"&="
	"|="
	"^="
	"<<="
	">>="
	"=&"
TernaryExpr ::=
	ConditionalOrExpr IfConditionalOrExpr*
IfConditionalOrExpr ::=
	"?" Expr ":" Expr
ConditionalOrExpr ::=
	ConditionalAndExpr ConditionalOrConditionalAndExpr*
ConditionalOrConditionalAndExpr ::=
	"||" ConditionalAndExpr
ConditionalAndExpr ::=
	BitOrExpr ConditionalAndBitOrExpr*
ConditionalAndBitOrExpr ::=
	"&&" BitOrExpr
BitOrExpr ::=
	BitXorExpr BitOrBitXorExpr*
BitOrBitXorExpr ::=
	"|" BitXorExpr
BitXorExpr ::=
	BitAndExpr BitXorBitAndExpr*
BitXorBitAndExpr ::=
	"^" BitAndExpr
BitAndExpr ::=
	EqualityExpr BitAndEqualityExpr*
BitAndEqualityExpr ::=
	"&" EqualityExpr
EqualityExpr ::=
	InequalityExpr EqualityOpInequalityExpr*
EqualityOpInequalityExpr ::=
	EqualityOp InequalityExpr
EqualityOp ::=
	"=="
	"!="
	"==="
	"!=="
InequalityExpr ::=
	ShiftExpr InequalityOpShiftExpr*
InequalityOpShiftExpr ::=
	InequalityOp ShiftExpr
InequalityOp ::=
	"<>"
	"<"
	"<="
	">"
	">="
ShiftExpr ::=
	CatExpr ShiftOpCatExpr*
ShiftOpCatExpr ::=
	ShiftOp CatExpr
ShiftOp ::=
	"<<"
	">>"
CatExpr ::=
	AddExpr CatAddExpr*
CatAddExpr ::=
	"." AddExpr
AddExpr ::=
	MultExpr AddOpMultExpr*
AddOpMultExpr ::=
	AddOp MultExpr
AddOp ::=
	"+"
	"-"
MultExpr ::=
	NotExpr MultOpNotExpr*
MultOpNotExpr ::=
	MultOp NotExpr
MultOp ::=
	"*"
	"/"
	"%"
NotExpr ::=
	"!"* InstanceExpr
InstanceExpr ::=
	UnaryExpr InstanceOfUnaryExpr?
InstanceOfUnaryExpr ::=
	"instanceof" UnaryExpr
UnaryExpr ::=
	UnaryOp* IncDecExpr
UnaryOp ::=
	"~"
	"-"
	"@"
	"(" CastType ")"
	"@" "!"
CastType ::=
	"int"
	"integer"
	"bool"
	"boolean"
	"real"
	"double"
	"float"
	"string"
	"array"
	"object"
	"unset"
IncDecExpr ::=
	IncDec* Primary IncDec*
IncDec ::=
	"++"
	"--"
Primary ::=
	VariableOrFunctionCall
	ScalarConstant
	Array
	List
	InternalFunction
	"(" Expr ")"
ScalarConstant ::=
	PlusMinus? NumericConstant
	StringConstant
	MagicConstant
	BooleanConstant
	"null"
PlusMinus ::=
	"+"
	"-"
NumericConstant ::=
	number
	Hexa
	Octa
StringConstant ::=
	stringlit
	charlit
	CommandString
	"<<<" id (IN NL) EncapsedInlineHTML* id EX
EncapsedInlineHTML ::=
	tag_token_or_key
popid ::=
	id
token_or_key ::=
	token
	key
BooleanConstant ::=
	"true"
	"false"
StaticScalar ::=
	Primary
Array ::=
	"array" "(" IN {ArrayPair ","}* ","? EX ")"
ArrayPair ::=
	NL ArrayKey? ArrayValue
ArrayKey ::=
	Expr "=>"
ArrayValue ::=
	Expr
	"&" ObjectCVar
VariableOrFunctionCall ::=
	ObjectCVar
ObjectCVar ::=
	ClassNameColonColon? CVar CallParams? ArrowObjectProperty*
ClassNameColonColon ::=
	ClassName "::"
CallParam ::=
	"&"? Expr
CallParams ::=
	"(" {CallParam ","}* ")"
ClassName ::=
	id
ArrowObjectProperty ::=
	"->" ObjectProperty CallParams?
ObjectProperty ::=
	VariableName
	IdOrKey
	ObjectProperty VariableExtension
	ObjectCVar
CVar ::=
	ReferenceVariable
ReferenceVariable ::=
	CompoundVariable VariableExtension*
VariableExtension ::=
	"[" Expr? "]"
	"{" Expr "}"
CompoundVariable ::=
	VariableName
VariableName ::=
	SimpleVariableName
	"$" VariableName
	"{" SimpleVariableName "}"
	"{" Expr "}"
SimpleVariableName ::=
	id
	"$" SPOFF IdOrKey SPON
IdOrKey ::=
	id
	key
List ::=
	"list" "(" {ListElem ","}* ")"
ListElem ::=
	List
	ObjectCVar
	ε
InternalFunction ::=
	"new" Primary
	"clone" Primary
	"isset" "(" {ObjectCVar ","}+ ")"
	"empty" "(" ObjectCVar ")"
	"intval" "(" ObjectCVar ")"
	"eval" "(" Expr ")"
	"exit" exit_value?
	"include" Expr
	"include_once" Expr
	"require" Expr
	"require_once" Expr
exit_value ::=
	"(" Expr? ")"

GrammarLabMaintained by Dr. Vadim Zaytsev a.k.a. @grammarware. Last updated in September 2015. []