scala syntax analysis
TRANSCRIPT
![Page 1: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/1.jpg)
Syntax analysis of ScalaCompiler construction
Speaker: Nurgaliev Ildar
![Page 2: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/2.jpg)
Basic Syntax
❖ Scala is case-sensitive
❖ Class names - For all class names, the first letter should be in Upper
Case.
❖ Method Names - All method names should start with a Lower Case letter.
❖ Program File Name - Name of the program file should exactly match the
object name.
![Page 3: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/3.jpg)
Characters are distinguished
according to the following classes
1. Whitespace characters: \u0020(space) \u0009(tab) \u000D(\r) \u000A(\n)
2. Letters: (Unicode categories) lower case letters(Ll), upper case letters(Lu), title-
case letters(Lt), other letters(Lo), letter numerals(Nl) and the two characters
‘$’ and ‘_’
3. Digits: ‘0’ . . . ‘9’
4. Parentheses: ( ) [ ] { }
5. Delimiter characters: ‘ ’ " . ; ,
6. Operator characters: all printable ASCII characters \u0020-\u007F, mathematical
symbols(Sm) and other symbols(So) except parentheses ([]) and periods
![Page 4: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/4.jpg)
Characters are distinguished
according to the following classes
Ex:
val #^ = 1 // legal - two opchars
val # = 1 // illegal - reserved word like class or => or @
val + = 1 // legal - opchar
val &+ = 1 // legal - two opchars
val &2 = 1 // illegal - opchar and letter do not mix arbitrarily
val £2 = 1 // working - £ is part of Sc (Symbol currency) - undefined by spec
val ¬ = 1 // legal - part of Sm
![Page 5: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/5.jpg)
Scala identifiers
4 types of identifiers supported by Scala:
● ALPHANUMERIC IDENTIFIERS
● OPERATOR IDENTIFIERS
● MIXED IDENTIFIERS
● LITERAL IDENTIFIERS
![Page 6: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/6.jpg)
ALPHANUMERIC IDENTIFIERS
● An ALPHANUMERIC IDENTIFIERS starts with a letter or underscore,
which can be followed by further letters, digits, or underscores.
● '$' character is a reserved keyword
Legal alphanumeric identifiers:
age, salary, _value, __1_value
Illegal identifiers:
$salary, 123abc, -salary
![Page 7: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/7.jpg)
OPERATOR IDENTIFIERS
● An operator identifier consists of one or more operator characters.
● Operator characters are printable ASCII characters such as +, :, ?, ~ or #.
Legal operator identifiers:
+ ++ ::: <?> :>
ps: The Scala compiler will internally "mangle" operator identifiers to turn them into legal Java
identifiers with embedded $ characters. For instance, the identifier :-> would be represented internally
as $colon$minus$greater method. This is consistent with Java anonymous class names.
![Page 8: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/8.jpg)
MIXED IDENTIFIERS
Consists of an alphanumeric identifier, which is followed
by an underscore and an operator identifier.
Legal mixed identifiers: unary_+, myvar_=
unary_+ as a method name defines a unary + operator
myvar_= as method name defines an assignment operator.
![Page 9: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/9.jpg)
MIXED IDENTIFIERSScala will only allow mixed identifier names (containing alphanumerics and punctuation) if you separate them by _.
Illegal identifiers:
scala> def iszero?(x : Int) = x == 0
<console>:1: error: '=' expected but identifier found.
def iszero?(x : Int) = x == 0
^
Legal identifiers:
scala> def iszero_?(x : Int) = x == 0
iszero_$qmark: (x: Int)Boolean
![Page 10: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/10.jpg)
LITERAL IDENTIFIERS
A literal identifier is an arbitrary string enclosed in back-quotes (` . . . `).
Legal literal identifiers:
- `x` `<clinit>` `yield`
- using back-quotes, you can more or less give any name to a field identifier.
val ` ` = 0
Back-quotes are necessary when Java identifiers clash with Scala reserved words:Thread.`yield`()
![Page 11: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/11.jpg)
LITERAL IDENTIFIERS
- Case statements. The convention is that Lower case names refer to match variables; Upper case names refer to identifiers from the outer
scope.
val A = "a"
val b = "b"
"a" match {
case b => println("b")
case A => println("A")
} //case A were unreachable
"a" match {
case `b` => println("b")
case A => println("A")
} // prints "A".
![Page 12: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/12.jpg)
Scala Keywords:abstract case catch class
def do else extends
false final finally for
forSome if implicit import
lazy match new null
object override package private
protected return sealed super
this throw trait try
true type val var
while with yield
- : = =>
<- <: <% >:
# @
![Page 13: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/13.jpg)
Comments in Scala
object HelloWorld {
/* This is my first Scala program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
* /* Nested comments are available */
*/
def main(args: Array[String]) {
// Prints Hello World
// This is also an example of single line comment.
println("Hello, world!")
}
}
![Page 14: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/14.jpg)
Newline Characters
Scala is a line-oriented language: statements may be terminated by
semicolons (;) or newlines.
def swap(i: Int, j: Int) {
val t = xs(i)
xs(i) = xs(j)
xs(j) = t()
}
Semicolon is required if you write multiple statements on a single line:
val s = "hello"; println(s)
![Page 15: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/15.jpg)
Scala mode and XML mode
● Start XML scanning when “<” is encountered
● Finish XML scanning after successful parse
● Scala expressions are embeddable in XML
● Need to keep stack of Scala/XML nesting
● No Scala tokens in XML mode
![Page 16: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/16.jpg)
Scala mode and XML mode
var xmlBook =
<book>
<title>Scala</title>
<version>{book.ver}</version>
</book>
![Page 17: Scala syntax analysis](https://reader035.vdocuments.net/reader035/viewer/2022071814/55a677aa1a28abb1758b46cc/html5/thumbnails/17.jpg)
References:
- The Scala Language Specification Version 2.9
Martin Odersky, 11 June 2014
- Scala for the impatient
Cay S. Hostman