|
Professor Sandwichthrower insists upon writing code in the obscure programming language Qadgop, so you have
been nominated to write a program to make her programs more easy to read in a web browser, by adding syntax highlighting
in HTML, as precisely defined below. No advance knowledge of HTML is necessary to solve this problem.
For the purposes of syntax highlighting a Qadgop program consists of a sequence of the following lexical entities.
- White Space: a sequence of tabs and spaces
- Comment: a # character followed by any text until the end of the line
- Reserved word: any of the following words:
- if unless then else elseif while until do for function method class return break next device yield
- Operator: any sequence of the characters ~!@$%^&*()-+={}[]|\:;<>,.?/
- Number: either a sequence of digits (e.g. 15920) or a floating point of one of the three forms as in C++ or
Java, e.g. 1e-17 or 2.43e-21 or 123.456, all without a leading minus sign
- String: This is doubly quoted as in C++ or Java with \ as an escape character, so \\ represents \, \"
represents " and there are other escapes such as \n where the \ is followed by a single character; no other
escape sequences are permitted.
- Identifier: any sequence of characters starting with a letter or underscore, followed by zero or more letters,
underscores and digits, that is not a Reserved word (defined above)
Your program should first output the following text, exact layout not being important. You may cut and paste
this from the browser.
<html><head><title>Qadgop Program</title>
<style type='text/css'>
body {
Font-Family : Courier New, Courier ;
Font-Size : Small
}
.Keyword {
Font-Weight : Bold
}
.String {
Color : #009900
}
.Operator {
Color : blue
}
.Number {
Color : #9900CC
}
.Comment {
Color : #993300 ;
Font-Style : Italic
}
</style>
</head>
<body>
Then it should write out the input converted to HTML as described below. And finally it should write out the
following text.
</body></html>
Each line of input is a sequence of the lexical elements described above. Each instance lexical element excepting
Identifier and Whitespace should be replaced by preceding it with a span tag and ending it with a closing span
tag exactly as in the following examples.
<span class="Keyword">while</span>
<span class="Number">123456</span>
<span class="String">"Hello"</span>
<span class="Operator">+:=</span>
<span class="Comment">#this is a comment</span>
Additionally, certain characters are not permitted in HTML, so if any of the characters <>&"
appear in the input, they are to be replaced by escape sequences as follows, before the above tags are inserted.
Replace " by "
Replace < by <
Replace > by >
Replace & by &
Finally, in an instance of Whitespace, each space or tab should be replaced by non-breaking spaces as follows.
Replace space by
Replace tab by
Each line of input becomes a line of output by applying the above transformations, and is then written out terminated
by the following tag followed by a newline.
<br>
Sample input and output will be provided.
|