languages in computer science
An English sentence can be regarded as a string of words (단어들의 나열),
and an English word can be regarded as a string of letters (문자들의 나열).
- Not every string of letters is a legitimate (올바른) word, and not every string of words is a grammatical sentence.
- Legitimate word? ⇒ dictionary. Grammar? ⇒ grammar book.
Computer languages are similar to English in that
- Certain strings of characters are legitimate words of the language and
- Certain strings of words can be put together according to certain rules to form syntactically correct programs
A compiler for a computer language analyzes the stream of characters in a program
- First to recognize individual word and sentence units (this part is called a lexical (어휘, 사전적인) scanner),
- Then to analyze the syntax, or grammar, of the sentences (this part is called a syntactic analyzer),
- and finally to translate the sentences into machine code (this part is called a code generator).
Example: a simple calculator language
Words
- Valid: Numbers (123), operators (+, -), and parentheses ((, )).
- Invalid: 12a3, ++.
Sentences
- Valid: 3 + 4, (2 * 5) - 1.
- Invalid: 3 +, 2 * (5 -
Calculator (Compiler) steps