-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ItaloCobains
committed
Nov 28, 2023
1 parent
9dba813
commit f2f682e
Showing
31 changed files
with
1,799 additions
and
241 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+43.2 KB
.sass-cache/18755931ac3c0b7fe879b6cdcc28e2f1d2d1847a/_syntax-highlighting.scssc
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,13 @@ title: ItaloCobainsBlog | |
email: [email protected] | ||
description: >- # this means to ignore newlines until "baseurl:" | ||
Bem-vindo ao nosso blog, um espaço dedicado à exploração do fascinante mundo da programação, tecnologia, hacking ético e ciência da computação. Mergulhe conosco em uma jornada repleta de insights e novidades, enquanto compartilhamos nosso conhecimento e paixão por esses campos em constante evolução. | ||
baseurl: "" # the subpath of your site, e.g. /blog | ||
url: "" # the base hostname & protocol for your site, e.g. http://example.com | ||
# Comentários do Disqus | ||
disqus: | ||
shortname: ItaloCobainsBlog | ||
# Deixe o nome abreviado em branco para desativar os comentários em todo o site. | ||
# Desative comentários para qualquer postagem adicionando `comments: false` ao YAML Front Matter dessa postagem. | ||
|
||
url: "http://ItaloCobainsBlog.com" # the base hostname & protocol for your site, e.g. http://example.com | ||
twitter_username: JustItalow | ||
github_username: ItaloCobains | ||
|
||
|
@@ -27,6 +32,7 @@ markdown: kramdown | |
theme: minima | ||
plugins: | ||
- jekyll-feed | ||
- jekyll-seo-tag | ||
|
||
# Exclude from processing. | ||
# The following items will not be processed, by default. Create a custom list | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{% if page.comments != false and jekyll.environment == "production" %} | ||
|
||
<div id="disqus_thread"></div> | ||
<script> | ||
var disqus_config = function () { | ||
this.page.url = '{{ page.url | absolute_url }}'; | ||
this.page.identifier = '{{ page.url | absolute_url }}'; | ||
}; | ||
(function () { | ||
var d = document, s = d.createElement('script'); | ||
s.src = 'https://{{ site.disqus.shortname }}.disqus.com/embed.js'; | ||
s.setAttribute('data-timestamp', +new Date()); | ||
(d.head || d.body).appendChild(s); | ||
})(); | ||
</script> | ||
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments | ||
powered by Disqus.</a></noscript> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<h1 id="o-que-é-um-lex-ou-avaliador-lexico-">O que é um <code class="language-plaintext highlighter-rouge">lex</code> ou <code class="language-plaintext highlighter-rouge">avaliador lexico</code> ?</h1> | ||
|
||
<p><code class="language-plaintext highlighter-rouge">Lexical analyzer</code> ou <code class="language-plaintext highlighter-rouge">lex</code> ou <code class="language-plaintext highlighter-rouge">avaliador léxico</code> é um processo fundamental na arquitetura de qualquer interpretador ou compilador. Ele é responsável por analisar o <code class="language-plaintext highlighter-rouge">codigo fonte</code> ou <code class="language-plaintext highlighter-rouge">entrada de texto</code> iterando sobre cada caractere é gerando <code class="language-plaintext highlighter-rouge">tokens</code> ou <code class="language-plaintext highlighter-rouge">simbolos</code> que vão ser importante para os próximos passos de um compilador/interpretador.</p> | ||
|
||
<h2 id="como-escrever-um-lex-">Como escrever um lex ?</h2> | ||
|
||
<p>Exemplo: LexEstupido.js</p> | ||
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nx">Token</span> <span class="p">{</span> | ||
<span class="kd">constructor</span><span class="p">(</span><span class="nx">type</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span> | ||
<span class="k">this</span><span class="p">.</span><span class="nx">type</span> <span class="o">=</span> <span class="nx">type</span><span class="p">;</span> | ||
<span class="k">this</span><span class="p">.</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span> | ||
<span class="p">}</span> | ||
<span class="p">}</span> | ||
|
||
<span class="kd">class</span> <span class="nx">Lexer</span> <span class="p">{</span> | ||
<span class="kd">constructor</span><span class="p">(</span><span class="nx">code</span><span class="p">)</span> <span class="p">{</span> | ||
<span class="k">this</span><span class="p">.</span><span class="nx">code</span> <span class="o">=</span> <span class="nx">code</span><span class="p">;</span> | ||
<span class="k">this</span><span class="p">.</span><span class="nx">position</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> | ||
<span class="p">}</span> | ||
|
||
<span class="nx">isWhitespace</span><span class="p">(</span><span class="nx">char</span><span class="p">)</span> <span class="p">{</span> | ||
<span class="k">return</span> <span class="sr">/</span><span class="se">\s</span><span class="sr">/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">char</span><span class="p">);</span> | ||
<span class="p">}</span> | ||
|
||
<span class="nx">isDigit</span><span class="p">(</span><span class="nx">char</span><span class="p">)</span> <span class="p">{</span> | ||
<span class="k">return</span> <span class="sr">/</span><span class="se">\d</span><span class="sr">/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">char</span><span class="p">);</span> | ||
<span class="p">}</span> | ||
|
||
<span class="nx">isAlpha</span><span class="p">(</span><span class="nx">char</span><span class="p">)</span> <span class="p">{</span> | ||
<span class="k">return</span> <span class="sr">/</span><span class="se">[</span><span class="sr">a-zA-Z_</span><span class="se">]</span><span class="sr">/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">char</span><span class="p">);</span> | ||
<span class="p">}</span> | ||
|
||
<span class="nx">getNextToken</span><span class="p">()</span> <span class="p">{</span> | ||
<span class="k">while</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span> <span class="o"><</span> <span class="k">this</span><span class="p">.</span><span class="nx">code</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span> | ||
<span class="kd">const</span> <span class="nx">char</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">code</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="p">];</span> | ||
|
||
<span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">isWhitespace</span><span class="p">(</span><span class="nx">char</span><span class="p">))</span> <span class="p">{</span> | ||
<span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="o">++</span><span class="p">;</span> | ||
<span class="k">continue</span><span class="p">;</span> | ||
<span class="p">}</span> | ||
|
||
<span class="k">switch</span> <span class="p">(</span><span class="kc">true</span><span class="p">)</span> <span class="p">{</span> | ||
<span class="k">case</span> <span class="k">this</span><span class="p">.</span><span class="nx">isDigit</span><span class="p">(</span><span class="nx">char</span><span class="p">):</span> | ||
<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">parseNumber</span><span class="p">();</span> | ||
<span class="k">case</span> <span class="k">this</span><span class="p">.</span><span class="nx">isAlpha</span><span class="p">(</span><span class="nx">char</span><span class="p">):</span> | ||
<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">parseIdentifier</span><span class="p">();</span> | ||
<span class="k">case</span> <span class="sr">/</span><span class="se">[\+\-\*\/\(\)]</span><span class="sr">/</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">char</span><span class="p">):</span> | ||
<span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="o">++</span><span class="p">;</span> | ||
<span class="k">return</span> <span class="k">new</span> <span class="nx">Token</span><span class="p">(</span><span class="nx">char</span><span class="p">,</span> <span class="nx">char</span><span class="p">);</span> | ||
<span class="k">case</span> <span class="nx">char</span> <span class="o">===</span> <span class="dl">'</span><span class="s1">=</span><span class="dl">'</span><span class="p">:</span> | ||
<span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="o">++</span><span class="p">;</span> | ||
<span class="k">return</span> <span class="k">new</span> <span class="nx">Token</span><span class="p">(</span><span class="dl">'</span><span class="s1">EQUALS</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">=</span><span class="dl">'</span><span class="p">);</span> | ||
<span class="nl">default</span><span class="p">:</span> | ||
<span class="nx">console</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="s2">`Erro léxico: Caractere inesperado '</span><span class="p">${</span><span class="nx">char</span><span class="p">}</span><span class="s2">' na posição </span><span class="p">${</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="p">}</span><span class="s2">`</span><span class="p">);</span> | ||
<span class="k">return</span> <span class="kc">null</span><span class="p">;</span> | ||
<span class="p">}</span> | ||
<span class="p">}</span> | ||
|
||
<span class="c1">// Fim do código</span> | ||
<span class="k">return</span> <span class="kc">null</span><span class="p">;</span> | ||
<span class="p">}</span> | ||
|
||
<span class="nx">parseNumber</span><span class="p">()</span> <span class="p">{</span> | ||
<span class="kd">let</span> <span class="nx">value</span> <span class="o">=</span> <span class="dl">''</span><span class="p">;</span> | ||
|
||
<span class="k">while</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span> <span class="o"><</span> <span class="k">this</span><span class="p">.</span><span class="nx">code</span><span class="p">.</span><span class="nx">length</span> <span class="o">&&</span> <span class="k">this</span><span class="p">.</span><span class="nx">isDigit</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">code</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="p">]))</span> <span class="p">{</span> | ||
<span class="nx">value</span> <span class="o">+=</span> <span class="k">this</span><span class="p">.</span><span class="nx">code</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="p">];</span> | ||
<span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="o">++</span><span class="p">;</span> | ||
<span class="p">}</span> | ||
|
||
<span class="k">return</span> <span class="k">new</span> <span class="nx">Token</span><span class="p">(</span><span class="dl">'</span><span class="s1">NUMBER</span><span class="dl">'</span><span class="p">,</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="mi">10</span><span class="p">));</span> | ||
<span class="p">}</span> | ||
|
||
<span class="nx">parseIdentifier</span><span class="p">()</span> <span class="p">{</span> | ||
<span class="kd">let</span> <span class="nx">value</span> <span class="o">=</span> <span class="dl">''</span><span class="p">;</span> | ||
|
||
<span class="k">while</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span> <span class="o"><</span> <span class="k">this</span><span class="p">.</span><span class="nx">code</span><span class="p">.</span><span class="nx">length</span> <span class="o">&&</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">isAlpha</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">code</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="p">])</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">isDigit</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">code</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="p">])))</span> <span class="p">{</span> | ||
<span class="nx">value</span> <span class="o">+=</span> <span class="k">this</span><span class="p">.</span><span class="nx">code</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="p">];</span> | ||
<span class="k">this</span><span class="p">.</span><span class="nx">position</span><span class="o">++</span><span class="p">;</span> | ||
<span class="p">}</span> | ||
|
||
<span class="k">return</span> <span class="k">new</span> <span class="nx">Token</span><span class="p">(</span><span class="dl">'</span><span class="s1">ID</span><span class="dl">'</span><span class="p">,</span> <span class="nx">value</span><span class="p">);</span> | ||
<span class="p">}</span> | ||
|
||
<span class="nx">tokenize</span><span class="p">()</span> <span class="p">{</span> | ||
<span class="kd">const</span> <span class="nx">tokens</span> <span class="o">=</span> <span class="p">[];</span> | ||
|
||
<span class="kd">let</span> <span class="nx">token</span><span class="p">;</span> | ||
<span class="k">while</span> <span class="p">((</span><span class="nx">token</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">getNextToken</span><span class="p">())</span> <span class="o">!==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span> | ||
<span class="nx">tokens</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">token</span><span class="p">);</span> | ||
<span class="p">}</span> | ||
|
||
<span class="k">return</span> <span class="nx">tokens</span><span class="p">;</span> | ||
<span class="p">}</span> | ||
<span class="p">}</span> | ||
|
||
<span class="c1">// Exemplo de uso</span> | ||
<span class="kd">const</span> <span class="nx">codigoFonte</span> <span class="o">=</span> <span class="dl">"</span><span class="s2">x = 10 + 20 * (30 - 5)</span><span class="dl">"</span><span class="p">;</span> | ||
<span class="kd">const</span> <span class="nx">lexer</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Lexer</span><span class="p">(</span><span class="nx">codigoFonte</span><span class="p">);</span> | ||
<span class="kd">const</span> <span class="nx">tokensResultantes</span> <span class="o">=</span> <span class="nx">lexer</span><span class="p">.</span><span class="nx">tokenize</span><span class="p">();</span> | ||
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">tokensResultantes</span><span class="p">);</span> | ||
|
||
</code></pre></div></div> | ||
|
||
<p>Primeiramente, criamos um ‘Token’, uma estrutura que armazena a identidade de cada caractere. É importante observar que um ‘Token’ pode conter mais atributos do que os exemplificados aqui; meu exemplo é simplificado.</p> | ||
|
||
<p>Em seguida, começamos com a classe que recebe o código no construtor. Posteriormente, o método ‘tokenize’ é implementado como o principal da classe, responsável por percorrer o código fonte. O método ‘getNextToken’ é encarregado de identificar o caractere atual e atribuí-lo a um ‘Token’. Os demais métodos servem apenas como validadores de strings. Ao final, o vetor de ‘Tokens’ é retornado.”</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.