As we saw in that last quiz,
it's not quite clear what to do when our token definitions overlap.
The 7-character sequence "hello"
matches our regular expression for word
but also matches our regular expression for string.
This is a problem not just with computer languages
but also with natural languages.
As the hypothetical owner of this restaurant would notice,
we don't just serve hamburgers, we serve people
could be interpreted the wrong way.
Presumably those hamburgers are soylent green flavored.
We want to have definitive rules for figuring out
which of these we prefer.
In fact, we're going to use a very simple rule.
The first one you list wins,
the one closer to the top of the file,
so this is our big winner and is going to take priority over string.
If you're making a lexical analyzer for HTML or JavaScript,
ordering your token definitions is of prime importance.
Let's investigate this issue in the form of a quiz.
Suppose we have the input string hello, "world,"
and we really want that to yield word,
the word hello, followed by a string.
I'm going to list 3 rules for you,
and I want you to tell me which one has to come last
for us to get the desired effect.
And here, because you've seen it all before, I'm eliding some of the details
like the colon, token, blah, blah, blah.
Instead what I'd like you to do is tell me
which one of these functions, which one of these rules,
would have to come last, bearing in mind that the one that comes first
wins all ties in order for hello, "world" to break down into
a word followed by a string.
前回の小テストで分かったように
トークンの定義が重複している時
どうすればいいか明確ではありません
“”がついた7つの文字から成るhelloは
単語として正規表現に一致しますが
文字列としての正規表現にも一致します
これはコンピュータ言語だけではなく
自然言語にも起こる問題です
レストランのオーナーが次のように言ったとします
“We don't just hamburgers,
we serve people!”
これは間違って解釈される可能性もあります
ハンバーガーは人間の味がするかもしれません
そこで2つのうちどちらを優先するか把握するため
決定的なルールを設ける必要があります
実際とても単純なルールを使用します
最初に来る定義が優先されます
ファイルの上に近い方です
そのため単語が文字列より
優先されることになります
HTMLやJavaScriptの字句解析プログラムを作る時
トークンの定義の順序が最も重要です
小テストでこの点を検証してみましょう
入力文字列としてhello、“world”があります
最初に単語つまりhello
次に文字列を得たいと思います
これから3つの定義を書きます
求められている結果を得るためには
どの定義が最後に来るのかを教えてください
すべて見たことがある定義ですので
(token):など詳細の一部を省いています
その代わり皆さんには
これらのどの関数や定義が
最後に来なければいけないのか答えてもらいます
最初に来るものが優先され
単語、文字列の順に分割されることを踏まえ
考えてみてください
Como vimos no último teste,
não é sempre claro o que vai acontecer quando nossas definições de token se sobrepõem.
A sequência de 7 caracteres "hello"
casa com nossa expressão regular para WORD
e também com nossa expressão regular para STRING.
Isso é um problema não apenas com linguagens de programação,
mas também com linguagens naturais.
Como o hipotético dono dests restaurante, você perceberia
que "We don't just serve hamburgers, we serve people"
poderia ser interpretado de maneira incorreta.
Acho que esses hamburgers são de soja :-)
Queremos ter regras definitivas, que nos permitam escolher
qual delas nós preferimos
De fato, vamos usar uma regra muito simples.
A primeira da lista ganha,
aquela que estiver mais no topo do arquivo.
Então, este é o nosso vencedor, e terá prioridade sobre STRING.
Se você está fazendo um analisador léxico para HTML ou JavaScript,
a ordem das definições é muito importante.
Vamos investigar isso na forma de um teste.
Suponha que temos como entrada o string -- hello, "world" --
e queremos que isso resulte em WORD --
a palavra hello -- seguido de STRING.
Vou dar para você 3 regras,
e quero que voc6e me diga qual delas tem que vir por último,
para que tenhamos o efeito desejado.
E aqui, porque você viu tudo isso antes, estou omitindo alguns detalhes,
como COLON, TOKEN etc.
O que eu quero é que você me diga
qual dessas funções, qual dessas regras,
teria que vir por último, tendo em mente que a que vem primeiro
tem prioridade, quando separamos hello, "world" em
um token WORD seguido de STRING.