SlideShare a Scribd company logo
Syntax analysis of Scala
Compiler construction
Speaker: Nurgaliev Ildar
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.
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
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
Scala identifiers
4 types of identifiers supported by Scala:
● ALPHANUMERIC IDENTIFIERS
● OPERATOR IDENTIFIERS
● MIXED IDENTIFIERS
● LITERAL IDENTIFIERS
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
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.
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.
MIXED IDENTIFIERS
Scala 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
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`()
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".
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
- : = =>
<- <: <% >:
# @
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!")
}
}
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)
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
Scala mode and XML mode
var xmlBook =
<book>
<title>Scala</title>
<version>{book.ver}</version>
</book>
References:
- The Scala Language Specification Version 2.9
Martin Odersky, 11 June 2014
- Scala for the impatient
Cay S. Hostman

More Related Content

PPTX
Scala fundamentals
PPTX
Chap1introppt2php(finally done)
PPT
7.1.intro perl
ODP
A Tour Of Scala
PDF
Scala jargon cheatsheet
PDF
Introduction to programming in scala
PDF
Scala cheatsheet
Scala fundamentals
Chap1introppt2php(finally done)
7.1.intro perl
A Tour Of Scala
Scala jargon cheatsheet
Introduction to programming in scala
Scala cheatsheet

What's hot (20)

PPTX
Variables
PPTX
Object-Oriented Programming with PHP (part 1)
PPTX
PPT
Perl tutorial
PPT
Scala
PPTX
Introduction in php part 2
PDF
Google06
PDF
Functional programming in Scala
PDF
Few simple-type-tricks in scala
PDF
PDF
Cs3430 lecture 15
PPTX
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
PDF
Demystifying Shapeless
PPT
Perl Basics with Examples
PPT
Php Chapter 2 3 Training
PPTX
PDF
Sorting arrays in PHP
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PPTX
FFW Gabrovo PMG - PHP OOP Part 3
PDF
Javascript Journey
Variables
Object-Oriented Programming with PHP (part 1)
Perl tutorial
Scala
Introduction in php part 2
Google06
Functional programming in Scala
Few simple-type-tricks in scala
Cs3430 lecture 15
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Demystifying Shapeless
Perl Basics with Examples
Php Chapter 2 3 Training
Sorting arrays in PHP
PHP - DataType,Variable,Constant,Operators,Array,Include and require
FFW Gabrovo PMG - PHP OOP Part 3
Javascript Journey
Ad

Viewers also liked (20)

PPTX
Lets explore singapore
TXT
으리카지노 ''SX797.COM'' 릴게임설명
PPTX
The Aquarium of Barcelona
DOC
Oceanarium Reimagined Activity Teacher Final
PPTX
C:\Users\Magy\Documents\Oceanarium Jacob Kwas
PPTX
Marine aquarium decoration and live rocks
DOC
Oceanarium Reimagined Students Sheet
PPTX
Shedd Aquarium
PPT
Oceanarium
PDF
The oceanarium
PPT
The role of zoos and aquariums in wildlife health
PPTX
Aquarium audit
PPTX
Microorganisms in Marine Environments
PPTX
A brief background & development history on klcc
PPTX
Petronas twin towers malaysia
ODP
Thesis presentation
PPTX
Petronas twin tower
PPTX
Petronas Towers
PPT
Petronas twin-towers
Lets explore singapore
으리카지노 ''SX797.COM'' 릴게임설명
The Aquarium of Barcelona
Oceanarium Reimagined Activity Teacher Final
C:\Users\Magy\Documents\Oceanarium Jacob Kwas
Marine aquarium decoration and live rocks
Oceanarium Reimagined Students Sheet
Shedd Aquarium
Oceanarium
The oceanarium
The role of zoos and aquariums in wildlife health
Aquarium audit
Microorganisms in Marine Environments
A brief background & development history on klcc
Petronas twin towers malaysia
Thesis presentation
Petronas twin tower
Petronas Towers
Petronas twin-towers
Ad

Similar to Scala syntax analysis (20)

PPTX
Complete Overview about PERL
PPT
Introduction to perl_lists
PPT
Introduction to Perl
PDF
Programming in scala - 1
PPT
Introduction to perl_control structures
PDF
Stepping Up : A Brief Intro to Scala
PPT
Introduction to perl scripting______.ppt
PPTX
Shell scripting
PPT
PERL.ppt
PDF
Scala in Places API
PDF
06 ruby variables
ODP
String Interpolation in Scala
PPT
SQL.ppt
PPTX
Core Java Tutorials by Mahika Tutorials
PDF
A Brief Introduction to Scala for Java Developers
PDF
Miles Sabin Introduction To Scala For Java Developers
PPTX
Class 8 - Java.pptx
PPTX
Unit 1-perl names values and variables
PPTX
Perl names values and variables
Complete Overview about PERL
Introduction to perl_lists
Introduction to Perl
Programming in scala - 1
Introduction to perl_control structures
Stepping Up : A Brief Intro to Scala
Introduction to perl scripting______.ppt
Shell scripting
PERL.ppt
Scala in Places API
06 ruby variables
String Interpolation in Scala
SQL.ppt
Core Java Tutorials by Mahika Tutorials
A Brief Introduction to Scala for Java Developers
Miles Sabin Introduction To Scala For Java Developers
Class 8 - Java.pptx
Unit 1-perl names values and variables
Perl names values and variables

More from Ildar Nurgaliev (7)

PDF
Presentation
PDF
PPTX
Анализ маркетинговой деятельности ООО “БАРС ГРУП”
PPTX
Fuzzy logic and application in AI
PPTX
Kotlin compiler construction (very brief)
PPTX
Artificial neural network
PPTX
Social dynamic simulation
Presentation
Анализ маркетинговой деятельности ООО “БАРС ГРУП”
Fuzzy logic and application in AI
Kotlin compiler construction (very brief)
Artificial neural network
Social dynamic simulation

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Nekopoi APK 2025 free lastest update
PPTX
ai tools demonstartion for schools and inter college
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
assetexplorer- product-overview - presentation
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Digital Strategies for Manufacturing Companies
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
history of c programming in notes for students .pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
medical staffing services at VALiNTRY
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Reimagine Home Health with the Power of Agentic AI​
Wondershare Filmora 15 Crack With Activation Key [2025
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Nekopoi APK 2025 free lastest update
ai tools demonstartion for schools and inter college
Digital Systems & Binary Numbers (comprehensive )
Computer Software and OS of computer science of grade 11.pptx
assetexplorer- product-overview - presentation
CHAPTER 2 - PM Management and IT Context
Navsoft: AI-Powered Business Solutions & Custom Software Development
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Digital Strategies for Manufacturing Companies
wealthsignaloriginal-com-DS-text-... (1).pdf
history of c programming in notes for students .pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
2025 Textile ERP Trends: SAP, Odoo & Oracle
medical staffing services at VALiNTRY
How to Migrate SBCGlobal Email to Yahoo Easily
Design an Analysis of Algorithms II-SECS-1021-03
Reimagine Home Health with the Power of Agentic AI​

Scala syntax analysis

  • 1. Syntax analysis of Scala Compiler construction Speaker: Nurgaliev Ildar
  • 2. 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.
  • 3. 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
  • 4. 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
  • 5. Scala identifiers 4 types of identifiers supported by Scala: ● ALPHANUMERIC IDENTIFIERS ● OPERATOR IDENTIFIERS ● MIXED IDENTIFIERS ● LITERAL IDENTIFIERS
  • 6. 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
  • 7. 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.
  • 8. 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.
  • 9. MIXED IDENTIFIERS Scala 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
  • 10. 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`()
  • 11. 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".
  • 12. 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 - : = => <- <: <% >: # @
  • 13. 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!") } }
  • 14. 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)
  • 15. 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
  • 16. Scala mode and XML mode var xmlBook = <book> <title>Scala</title> <version>{book.ver}</version> </book>
  • 17. References: - The Scala Language Specification Version 2.9 Martin Odersky, 11 June 2014 - Scala for the impatient Cay S. Hostman

Editor's Notes

  • #2: http://www.tutorialspoint.com/scala/scala_basic_syntax.htm
  • #3: Case Sensitivity - Scala is case-sensitive, which means identifier Hello and hellowould have different meaning in Scala. Class Names - For all class names, the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. Example class MyFirstScalaClass Method Names - All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case. Example def myMethodName() Program File Name - Name of the program file should exactly match the object name. When saving the file you should save it using the object name (Remember scala is case-sensitive) and append '.scala' to the end of the name. (if the file name and the object name do not match your program will not compile). Example: Assume 'HelloWorld' is the object name. Then the file should be saved as'HelloWorld.scala' def main(args: Array[String]) - Scala program processing starts from the main() method which is a mandatory part of every Scala Program.
  • #7: An alphanumeric identifier starts with a letter or underscore, which can be followed by further letters, digits, or underscores. The '$' character is a reserved keyword in Scala and should not be used in identifiers. Following are legal alphanumeric identifiers: age, salary, _value, __1_value Following are illegal identifiers: $salary, 123abc, -salary
  • #8: An operator identifier consists of one or more operator characters. Operator characters are printable ASCII characters such as +, :, ?, ~ or #. Following are legal operator identifiers: + ++ ::: <?> :> 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.
  • #9: A mixed identifier consists of an alphanumeric identifier, which is followed by an underscore and an operator identifier. Following are legal mixed identifiers: unary_+, myvar_= Here, unary_+ used as a method name defines a unary + operator and myvar_= used as method name defines an assignment operator.
  • #11: A literal identifier is an arbitrary string enclosed in back ticks (` . . . `). Following are legal literal identifiers: `x` `<clinit>` `yield` The literal definition of identifiers is useful in two cases. The first case is, when there is already a reserved word of the same name in Scala and you need to use a Java library which does not care about that (and of course, why should it). using backticks, you can more or less give any name to a field identifier. val ` ` = 0 which defines a variable with name (one character of whitespace).
  • #12: The other use case comes with case statements. The convention is that lower case names refer to match variables, whereas upper case names refer to identifiers from the outer scope. So, val A = "a" val b = "b" "a" match { case b => println("b") case A => println("A") } prints "b" (if the compiler were dumb enough not to fail with saying case A were unreachable). If you want to refer to the originally defined val b, you need to use backticks as a marker. "a" match { case `b` => println("b") case A => println("A") } Which prints "A".
  • #14: Scala supports single-line and multi-line comments very similar to Java. Multi-line comments may be nested, but are required to be properly nested. All characters available inside any comment are ignored by Scala compiler.
  • #15: Scala is a line-oriented language where statements may be terminated by semicolons (;) or newlines. A semicolon at the end of a statement is usually optional. You can type one if you want but you don't have to if the statement appears by itself on a single line. On the other hand, a semicolon is required if you write multiple statements on a single line: val s = "hello"; println(s)