Emil Sekerinski, McMaster University, Winter Term 16/17 COMP SCI

EmilSekerinski,McMasterUniversity,WinterTerm16/17
COMPSCI1MD3IntroductiontoProgramming
Thesemanticsassignsameaningtosyntacticallycorrect
sentences;thesyntaxisdefinedontwolevels:symbols(words,
tokens)intermsofcharactersandsentencesintermsof
symbols.
English
Python
banan
3dist
incorrectsymbol
Kevinbanana
3 dist +
correctsymbols,incorrect
sentence
Thebananaruns
3 / "abc"
correctsymbols,correct
sentence,meaningless
Kevineatsabanana
time + dist / speed
correctsymbols,correct
sentence,meaningful
SentencesinEnglishaswellasinprogramminglanguageshave
ahierarchicalstructurethatcanbevisualizedbyan(upsidedown)syntaxtree
Kevineatsa banana
time + dist / speed
Thetheoryofformallanguagesoriginatesinlinguistics.
NoamChomsky,anAmericanlinguist,observedhowchildrenlearnlanguage.He
concludedthathumanshaveaninnateabilitytodeducelinguisticsrules.Thisis
evidentwhenchildrenovergeneralizetheserulesandreferto"mice"as"mouses"
orto"ran"as"runned".
Agrammardefinesthesyntactically
correctsentences:
isa
sentence
verbphrase
nounphrase
nounphrase
propernoun verb determiner noun
sentence::=nounphraseverbphrase
nounphrase::=propernoun|determinernoun
Kevin
eats
a
banana
verbphrase::=verb[nounphrase]
propernoun::=“Kevin”|“Dave”
optional
determiner::=“a”|“the”
or
sentence
noun::=“banana”|“apple”
verb::=“eats”|“runs”
literally
nounphrase verbphrase
propernoun
verb
Dave
runs
Agrammardefinesthesyntactically
correctsentences:
sentence
nounphrase verbphrase
determiner noun
sentence::=nounphraseverbphrase
nounphrase::=propernoun|determinernoun
verbphrase::=verb[nounphrase]
propernoun::=“Kevin”|“Dave”
determiner::=“a”|“the”
noun::=“banana”|“apple”
verb::=“eats”|“runs”
the
verb
runs
banana
sentence
verbphrase
nounphrase
nounphrase
propernoun verb determiner noun
Kevin
eats ?
banana
StartingwithAlgol60,itiscommontowritegrammarsof
programminglanguageinextendedBNF:
"x"
xliterally
A|B AorB
(A)
grouping
[A]
optionallyA
A*
Arepeatedzeroormoretimes
A+
Arepeatedoneormoretimes
Anidentifieristhenameofavariableorfunction:
identifier::=letter(letter|digit)*
number::=digit+
digit::="0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"
letter::="A"|…|"Z"|"a"|…|"z"|"_"
¡ 
¡ 
¡ 
¡ 
¡ 
¡ 
¡ 
identifier::=letter(letter|digit)*
number::=digit+
digit::="0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"
letter::="A"|…|"Z"|"a"|…|"z"|"_"
ITBAB102
4.5
1280MainStreet
0001
_5_3_0
U-2
OP
statement::=stmt_listNEWLINE|compound_stmt
stmt_list::=simple_stmt(";"simple_stmt)*[";"]
simple_stmt::=assignment_stmt|return_stmt|import_stmt
assignment_stmt::=identifier_list"="expression_list
identifier_list::=identifier(","identifier)*
expression_list::=expression(","expression)*
Examplesforstatements(NEWLINEimplicit):
¡  x=5
¡  x=5;
¡  x,y=5,7
¡  x=5;y=7
Howabout:A:yesB:no
¡  x,y=z
compound_stmt::=if_stmt|while_stmt|for_stmt|funcdef
if_stmt::="if"expression":"suite
("elif"expression":"suite)*
["else"":"suite]
suite::=stmt_listNEWLINE|NEWLINEINDENTstatement+DEDENT
Examples:
Howabout?A:yesB:no
•  ifa:ifb:x=5
•  ifa:x=5;y=7 •  ifa:
•  ifa:x=5
•  ifa:
ifb:x=5
y=7
x=5;y=7
elifc:y=7
•  ifa:x=5
•  ifa:
else:z=9
elsey=7
x=5
•  ifa:x=5
else:y=7
elifb:y=7
expression::=a_expr
a_expr::=m_expr|a_expr"+"m_expr|a_expr"-"m_expr
expression
m_expr::=u_expr|m_expr"*"u_expr|m_expr"//"u_expr|
m_expr"/"u_expr|m_expr"%"u_expr
a_expr
u_expr::=power|"-"u_expr|"+"u_expr
power::=atom["**"u_expr]
atom::=identifier|integer|"("expression")"
a_expr
m_expr
m_expr
m_expr
u_expr
u_expr
u_expr
power
power
power
a_expr:(binary)additiveexpression
atom
atom
atom
m_expr:(binary)multiplicativeexpression
identifer
u_expr:unaryexpression
identifer
identifer
Thefullgrammarisathttp://docs.python.org/3/reference time + dist / speed