category
stringclasses
107 values
title
stringlengths
15
179
question_link
stringlengths
59
147
question_body
stringlengths
53
33.8k
answer_html
stringlengths
0
28.8k
__index_level_0__
int64
0
1.58k
LSTM
Good AI model for learning to write code, specifically generate css from any given html?
https://cs.stackexchange.com/questions/120408/good-ai-model-for-learning-to-write-code-specifically-generate-css-from-any-giv
<p>Currently I am working on getting lots of html code and its related css, via id or class name. Once I have enough data to work with I am unsure how it would be easiest for any model to learn what css should be generated for each element.</p> <p>My idea is to make a script to extract all html elements from a given f...
300
LSTM
What are the limitations of RNNs?
https://cs.stackexchange.com/questions/53552/what-are-the-limitations-of-rnns
<p>For a school project, I'm planning to compare Spiking Neural Networks (SNNs) and Deep Learning recurrent neural networks, such as Long Short Term Memory (LSTMs) networks in learning a time-series. I would like to show some case where SNNs surpass LSTMs. Consequently, what are the limitations of LSTMs? Are they robus...
<p>I finally finished the project. Given really short signals and a really small training set, SNNs (I used <a href="http://minds.jacobs-university.de/sites/default/files/uploads/papers/EchoStatesTechRep.pdf" rel="nofollow noreferrer">Echo State Machines</a> and a <a href="http://ieeexplore.ieee.org/document/7378880/" ...
301
LSTM
Neural network: noisy temporal sequence converter (transducer?producer?) on demand?
https://cs.stackexchange.com/questions/22666/neural-network-noisy-temporal-sequence-converter-transducerproducer-on-dema
<p>I start to suspect this problem is very hard now that I cannot find a single relevant literature on the subject, but it's too late to change the class project topics now, so I hope any pointers to a solution. Please pardon the somewhat artificial scenerio of this question, but here goes:</p> <p>Technical version: <...
<p>I unfortunately know very little of neural network. The closest thing that your project reminds me of is speech recognition, and I would look at that literature. I am thinking of the first stage of speech recognition, when the sound stream is transformed into a word lattice (or a word stream, if you keep only the m...
302
LSTM
Machine learning for recommendation systems (feed forward and recurrent neural networks)
https://cs.stackexchange.com/questions/88401/machine-learning-for-recommendation-systems-feed-forward-and-recurrent-neural-n
<p>I recently started to learn about machine learning. I have created a feed forward neural network (ffnn) and a recurrent neural network (rnn) to predict user ratings of movies. I am using a subset of 2000 users and their ratings of the "Netflix Prize" dataset.</p> <p>The ffnn as well as the rrn have an accuracy of ~...
303
tokenization
Tokenization Problem
https://cs.stackexchange.com/questions/84928/tokenization-problem
<p>Yes, this is a quiz question. It's from a self-paced course, but the answer just isn't correct to me no matter how I look at it. There isn't really an active community to consult. </p> <p>My Regular Expression experience is profoundly in JavaScript and I'm concerned that it poisoned my thinking of Regular Expressio...
<p>Tokenisation does not search. [Note 1]. At each step, it matches (and consumes) a match of one of the patterns starting precisely at the current input point.</p> <p>Although there are other tokenisation algorithms, the most common one -- usually called <a href="https://en.wikipedia.org/wiki/Maximal_munch" rel="nofol...
304
tokenization
Tokenizer and complex operators
https://cs.stackexchange.com/questions/13418/tokenizer-and-complex-operators
<p>I'm trying to create simple tokenizer to transform following (only part shown) search expression to tokens</p> <pre><code>word1 near(1) word2 </code></pre> <p>where word1, word2 are some words and near(1) is distance operator. The question is how this expression should be tokenized. I see two ways</p> <pre><code...
<p>Since you indicate that the parameter of the near operator can be an arbitrary expression, it should be handled at the parser rather than at the lexer. Otherwise how would you handle things like near(x+y)?</p>
305
tokenization
Amortised cost - transferring tokens
https://cs.stackexchange.com/questions/164880/amortised-cost-transferring-tokens
<p>I'm trying to solve a problem from one of the older exams.</p> <p>Question:</p> <blockquote> <p>There's an infinite, one-dimensional board, with fields numbered consecutively <span class="math-container">$\ldots, -2, -1, 0, 1, 2, \ldots$</span> A move in the game consists of selecting a field and placing a token on ...
<p>To prove a lower bound simply take a worst case example. We will prove the following:</p> <p><strong>Induction Hypothesis:</strong> There exists a sequence of moves that results in <span class="math-container">$n$</span> tokens at position <span class="math-container">$\log n$</span> that requires at least <span cla...
306
tokenization
Can parser split tokens?
https://cs.stackexchange.com/questions/161493/can-parser-split-tokens
<p>Is it valid to have the token split up, in a parser; as shown in the below grammar:</p> <pre><code>expr -&gt; expr addop term term -&gt; term mulop factor factor -&gt; factor digit | digit addop -&gt; + | - mulop -&gt; * | / digit -&gt; 0 | 1 | ... | 9 </code></pre> <p>Say, for the arithmetic expression:...
<p><s>The arithmetic expression you provided <code>12 + 7 * 45 / 9 - 6 + 5</code> will cause the parser to return syntax error because <code>12</code> is not part of the language (same problem for <code>45</code>...). Specifically, it is not a token and will be recognized as the symbol <code>1</code> followed by <code>...
307
tokenization
How are lexical tokens produced
https://cs.stackexchange.com/questions/142035/how-are-lexical-tokens-produced
<p>I am studying Compiler Design. The instructor told us that when a program is given to lexical analyzer it find all tokens then a symbol table is created and it is updated at every phase accordingly, but I read this online <a href="https://www.radford.edu/%7Enokie/classes/380/phases.html" rel="nofollow noreferrer">no...
<p>Yes, normally a parser calls the lexical analyser every time it needs a token, and this results in many, many, many calls to the lexical analyser. It is well known by compiler writers that the lexical analysis can consume the larger proportion of the compilers execution time.</p> <p>However, the lexical analysis pro...
308
tokenization
What is a malformed token?
https://cs.stackexchange.com/questions/3278/what-is-a-malformed-token
<p>I am reading Programming Language Pragmatics by Michael Scott. He says that on a first pass, a compiler will break a program into a series of tokens. He says that it will check for malformed tokens, like 123abc or $@foo (in C). </p> <p>What is a malformed token? A variable that does not meet the rules of variable-n...
<p>The basic syntactic units in any <em>textual</em> programming language are tokens, which are, if you wish, the words in the programming language. The compiler parses these tokens to build sentences and so forth.</p> <p>A malformed token is a string of characters that is not a valid word for the programming language...
309
tokenization
Algorithm for token replacement game
https://cs.stackexchange.com/questions/120051/algorithm-for-token-replacement-game
<p>I'm having problems finding an algorithm to the following problem:</p> <p>A and B take turns replacing a number <span class="math-container">$n$</span> of tokens with either <span class="math-container">$floor((n+1)/2)$</span> or <span class="math-container">$n-1$</span>. The player who makes <strong>one</strong> t...
<p>I'd check what happens for some small(ish) values of initial <span class="math-container">$n$</span>, working up. If you know who wins if there are at most <span class="math-container">$n$</span> on the table, working out what happens with <span class="math-container">$n + 1$</span> is easy.</p>
310
tokenization
Are there any languages without tokens?
https://cs.stackexchange.com/questions/81412/are-there-any-languages-without-tokens
<p>I know a lot of computer languages and they all use tokens. E.g. in very early BASIC you could say <code>LET answer = 42</code>, which is composed of seven tokens, <code>LET</code>, <code>answer</code>, <code>=</code>, <code>42</code>, and three space tokens.</p> <p>It seems that every character must be part of som...
<p>I agree with the commenters that your question is either ill-defined or nonsensical. It all hinges on what you mean by "tokens", and what it would mean to "not have tokens."</p> <p>If you mean multi-character sequences that are "chunked" together into larger words, consider <a href="https://en.wikipedia.org/wiki/Br...
311
tokenization
Doubt on Token in Compiler Design
https://cs.stackexchange.com/questions/111752/doubt-on-token-in-compiler-design
<p>Say i have code snippet like --> </p> <p>m -= n;</p> <p>is <strong>minus</strong> and <strong>assignment</strong> considered as a single token or they will be considered as different token?</p> <p>So the total token count will be 4 or 5?</p>
<p>In the C language you cant insert space between '-' and '=', so you are better to implement '-=' as the single lexeme.</p>
312
tokenization
Do we need regular expression first or finite state automata in lexical anlysing?
https://cs.stackexchange.com/questions/84560/do-we-need-regular-expression-first-or-finite-state-automata-in-lexical-anlysing
<p>I'm a bit confused about the concept of finite state automata (FSA) and regular expression (RE) in lexical analysis. I have reading some books about compiler construction. At the part of tokenization, all the books I read talk about the regular expression first to recognize the tokens. For example, the regex below ...
<p>A regular expression is a language used to describe a finite state automaton. It allows you to define the fsa without drawing nodes and edges all over the place. The two go hand in hand in that regard.</p>
313
tokenization
how to create tokens from CFG
https://cs.stackexchange.com/questions/67961/how-to-create-tokens-from-cfg
<p>i have a context free grammar </p> <p>i want to create a tokens from the language </p> <p>is there any techniques to do that ? </p> <p>for example , this CFG from Prof.Alex Aiken notes : </p> <pre><code> D -&gt; D ; P | D D -&gt; def id(ARGS) = E ; ARGS -&gt; id, ARGS | id E -&gt; int | id | if E1 = E2 the...
<p>The problem of creating a parse tree given a grammar and a word generated by it is known as <em>parsing</em>. The <a href="https://en.wikipedia.org/wiki/CYK_algorithm" rel="nofollow noreferrer">CYK algorithm</a> parses all context-free grammars, but is slow. In practice, only restricted types of context-free grammar...
314
tokenization
Transition diagram for following token number
https://cs.stackexchange.com/questions/100705/transition-diagram-for-following-token-number
<p><strong>number -> digits(.digits)?(E[+ -]?digits)?</strong> This transition diagram runs the above mentioned token I can not understand this how this is work.what is meaning of "?"Symbol and digits. can anyone explain me this</p> <p><a href="https://i.sstatic.net/Rxc5U.png" rel="nofollow noreferrer"><img src="htt...
<p><strong>digits(.digits)?(E[+ -]?digits)?</strong> is a regular expression for the set of strings that the pictured automaton accepts. That is, all of the strings accepted by the automaton will match this pattern. If you're unfamiliar with regular expression syntax, you can check out <a href="https://en.wikipedia.org...
315
tokenization
Grammar where tokens can be transmuted
https://cs.stackexchange.com/questions/154435/grammar-where-tokens-can-be-transmuted
<p>I have a grammar which is mostly <code>LL(1)</code>, save for the fact that some tokens may be promoted to larger integer types.</p> <p>For example, let take the following grammar</p> <pre><code>S ::= terminal1 S1 S1 ::= integer_16 S2 | integer_32 S3 </code></pre> <p>where <code>S2</code> and <code>S3</code> can sha...
<p>The type of a literal integer is semantic, not syntactic, and should not be part of syntactic analysis. In other words, the program text can be <em>parsed</em> -- broken into syntactic parts with known relationships -- without knowing the magnitude of each integer literal. The magnitude of the literal does not affec...
316
tokenization
Product of Lexical Specification
https://cs.stackexchange.com/questions/103658/product-of-lexical-specification
<p>I have a problem that asks me to consider the string abbbaacc. I'm supposed to figure out which of the following lexical specification produces the tokenization ab/bb/a/acc.</p> <p>The options are:</p> <pre><code>A. a(b+c)* b+ B. ab b+ ac* C. c* b+ ab ac* D. b+ ab*...
<p>There's not enough information here to answer the question, but I can prove a pretty good guess based on how tokenizers are typically implemented. Generally the tokenizer will run through the list of patterns, trying each one I sequence. If one of them matches it takes the longest possible match as a token, then s...
317
tokenization
What is token-type in Lexical analysis?
https://cs.stackexchange.com/questions/47708/what-is-token-type-in-lexical-analysis
<p>I'm currently studying compiler construction book "Compilers Principles, Techniques, and Tools (2nd Edition)" , in page <a href="https://books.google.com.pk/books?id=kLVv4MSa7EUC&amp;pg=PA113&amp;lpg=PA113&amp;dq=%3Cid,%20pointer%20to%20symbol-table%20entry%20for%20M%3E%20%3Cmult%20op%3E%20%3Cid,%20pointer%20to%20sy...
<p>I don't know what is the proper name for what you're seeing, but it is the standard way to lexically analyze text. You divide it into tokens of specific types. For the sake of context-free parsing (the next step in the parsing chain), you only need the <em>type</em> of each lexeme; but further steps down the road wi...
318
tokenization
on-the-fly decompress a flat-file database
https://cs.stackexchange.com/questions/123604/on-the-fly-decompress-a-flat-file-database
<p>I'm facing the following problem. I have a flat-file database (e.g. CSV). Since it's relatively large to store in memory, I'd like to compress it.</p> <p>Given a key, I need to return the uncompressed text (record of values).</p> <p>So one naive idea is to tokenize the text into <strong>words</strong> and to have ...
<p>What is "too large to fit in memory"? Current operating systems can handle processes with GiB of memory, if you use e.g. mmap(3) on Unix/Linux, you can work as if you had the whole file in memory and access it at random. That might be much faster than compressing/uncompressing on the fly. And (if I understand your q...
319
tokenization
Should a lexer (tokenizer) handle unknown operators?
https://cs.stackexchange.com/questions/145273/should-a-lexer-tokenizer-handle-unknown-operators
<p>I have a list of supported operators, my question is whether the lexer should just yield the token for the operator or raise a syntax error in case that particular operator (let's say &quot;?&quot;) doesn't exist in the operators list?</p> <p>for example, the operators list [+, -]. for the expression &quot;1 ? 2&quo...
320
tokenization
Counting tokens in compilers, lexical analyser
https://cs.stackexchange.com/questions/97180/counting-tokens-in-compilers-lexical-analyser
<p>Let's start with the question. Say I have a C language statements follows</p> <ol> <li><p>it 458cat 2.01 = 96.87abc a.2 ;</p> <p>-my question is how many tokens are there in the above statements. Secondly does white space like tabs, newline, makes a token or not? </p></li> <li>If you are interested in my solution...
<p>In C, <code>458cat</code> is a single <em>ppnumber</em> token. It's not a valid number, so it will eventually produce an error message, but it is tokenised as a single token.</p> <p>There's a longer explanation of this behaviour <a href="https://stackoverflow.com/a/49365061/1566221">in this StackOverflow answer</a>...
321
tokenization
Morphing Hypercubes, Token Sliding and Odd Permutations
https://cs.stackexchange.com/questions/107018/morphing-hypercubes-token-sliding-and-odd-permutations
<p>A month ago, I asked the following question math.exchange (<a href="https://math.stackexchange.com/questions/3127874/morphing-hypercubes-and-odd-permutations">https://math.stackexchange.com/questions/3127874/morphing-hypercubes-and-odd-permutations</a>), but for completeness, I will include the details here.</p> <h...
322
tokenization
How to find optimal token set for compression?
https://cs.stackexchange.com/questions/109160/how-to-find-optimal-token-set-for-compression
<p>By token I mean the smallest element of source data, that the compression algorithm works on. It may be a bit (like in DMC), a letter (like in Huffman or PPM), a word, or variable-length string (like in LZ). </p> <p>(Please feel free to correct me on this term, if I use it incorrectly.)</p> <p>I'm thinking: what i...
<blockquote> <p>Are there existing algorithms for it? Are there any algorithms related to the problem?</p> </blockquote> <ul> <li>Theoretical one: <a href="https://en.wikipedia.org/wiki/Sequitur_algorithm" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Sequitur_algorithm</a></li> <li>Practical one: <a href=...
323
tokenization
Find first occurence of multiple tokens using C++
https://cs.stackexchange.com/questions/113376/find-first-occurence-of-multiple-tokens-using-c
<p>I am using C++03. I have a stream of chars and I need to find the first token from a group of tokens. That is, I need to find the lowest indexed match. Specifically, the stream is just an char array, and the tokens of interest are listed below. The start of a token can occur anywhere in the stream (and not necessari...
<p>You can turn that search into a DFA,and you can then pass the input through the DFA until an accepting state is reached (or the end of the string is encountered).</p> <p>Each pattern is turned into an NFA by adding a start state which self-loops on any input and transitions to the next state on a match of the firs...
324
tokenization
C language tokenizer output for static integer array
https://cs.stackexchange.com/questions/161252/c-language-tokenizer-output-for-static-integer-array
<p>Unable to find out how the C-language lexical analyzer would tokenize the declaration of a static array. Say, int i[3]= {1,2,3};</p> <p>The lexical analyzer would need to differentiate between just an integer identifier i, and a static array of size 3.</p> <p>Am confused how the lexical analyzer would tokenize.</p> ...
<p>In my answer I assume you are acquainted with formal grammars, especially, the Backus Naur form.</p> <p>The lexical analyzer, alone, could never state the sentence you provided actually belongs to C. Telling whether it belongs or not to the language, is a joint duty of the lexical and syntactical analyzers.</p> <p>O...
325
tokenization
Token Scanner for programming Language(Lexical Analysis)
https://cs.stackexchange.com/questions/101648/token-scanner-for-programming-languagelexical-analysis
<p><a href="https://i.sstatic.net/vKd0k.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vKd0k.png" alt="enter image description here"></a> This DFA is a token scanner for a programming language.I would like to add keywords of the programming language(if,else,end ... etc) in the DFA so the lexical analyze...
<p>There's a common misunderstanding about why many construction algorithms (and most notably Thompson's) use ε-NFAs. It's not because it's <em>necessary</em> to do so, and it's not because it's <em>efficient</em> to do so.</p> <p>Thompson's construction has the following advantages:</p> <ul> <li>It's recursive on th...
326
tokenization
A 9 token game of Nim tree construction
https://cs.stackexchange.com/questions/53745/a-9-token-game-of-nim-tree-construction
<p>Trying to construct the full tree for a 9 game token of Nim and am slightly confused. I don't understand how two players, min and max, will make their pick. For example, max picks first and can only pick [9]. Min then picks from [8-1], [7-2], [6-3], and [5-4]. How does min calculate it's utility value here?</p> <p...
327
tokenization
Lexical analysis on a series of tokens given regexes
https://cs.stackexchange.com/questions/148929/lexical-analysis-on-a-series-of-tokens-given-regexes
<p>I am to parse through a series of strings with a given token list. I was wondering if my lexical analysis is correct.</p> <pre><code>T1 = { abc, abc1 } T2 = { abd, abd1 } ID = [a-z]+[a-z0-9] NUM = 0 | [1-9][0-9] </code></pre> <p><code>lexer.getToken()</code> will return the current token and advance the input buffer...
<p>I think it's odd to say that &quot;1 indicates the next possible token&quot; when you intend to return the token <em>after</em> the first unconsumed token. Or do you allow <span class="math-container">$peek(0)$</span> to return the current token without consuming it? (Far and away the most common use for a peek func...
328
tokenization
Algorithm to generate a token as survey summary
https://cs.stackexchange.com/questions/128215/algorithm-to-generate-a-token-as-survey-summary
<p>I'm searching for an algorithm but struggle to find anything, as I'm not sure how to formulate it correctly. I created a simple survey app in Angular with 24 questions and each has 2-5 answers. When the user answered all the questions I'd like to give him a token (as short as possible) on the result page that he can...
<p>I think you should try converting your 24-digits (in base 10) number, in another base, which would reduce a lot the length of your token, while keeping the same information.</p> <p>You should try experimenting with this website for example, which convert in base 36 (10 number + 26 letters):</p> <p><a href="http://ww...
329
tokenization
TOPS trillion operations per second to Tokens per second
https://cs.stackexchange.com/questions/167711/tops-trillion-operations-per-second-to-tokens-per-second
<p>A lot of AI hardware coming out lately has its performance mentioned in TOPS i.e trillion operations per second.</p> <p>Does anyone have an Idea how to estimate the llm performance on such hardware in tokens per second.</p> <p>for example I have a hardware of 45 TOPS performance. if I perform inferencing of a 7 bill...
<p>You can find a <em>lot</em> written on the Internet on the number of tokens/second you can expect from different GPUs. I suggest you pick a specific LLM (e.g., Llama 2) and a specific GPU or hardware, and do some searching. Many people report their experience.</p> <p>There is no simple formula. So you'll need to ...
330
tokenization
Move tokens from s to t as fast as possible
https://cs.stackexchange.com/questions/45396/move-tokens-from-s-to-t-as-fast-as-possible
<p>Let $G=(V, E)$ be an unweighted and undirected graph, and $s, t \in E$. </p> <p>The problems starts with $n$ tokens on $s$. </p> <p>The goal is to move theses tokens to $t$ in a minimum of rounds with these rules :</p> <ul> <li>Each token can be moved up to once per round (a movement being when you transfer the t...
<p>Menger's theorem states that the maximum number of $s$-$t$ (internally) vertex-disjoint paths is equal to the minimum size of an $s$-$t$ vertex cut. Let the common value be $m$. Then it can be shown that the asymptotic number of rounds required is $n/m + O(1)$.</p> <p>Using the vertex disjoint paths, we can route ...
331
tokenization
Trying to understand a token file for lexical analysis
https://cs.stackexchange.com/questions/13909/trying-to-understand-a-token-file-for-lexical-analysis
<p>I am reading <a href="http://gnuu.org/2009/09/18/writing-your-own-toy-compiler/3/" rel="nofollow">this</a> article about compiler. I am facing some problem in understanding the content of the token file. Specifically, what is the meaning of the following lines: </p> <pre><code> [ \t\n] ; [a-z...
<p><code>*</code> means that any number (including zero) of preceding expression can occur</p> <p><code>+</code> means that at least one instance of preceding expression must occur</p>
332
tokenization
Explain better how CompuBERT handles math tokens on input
https://cs.stackexchange.com/questions/144403/explain-better-how-compubert-handles-math-tokens-on-input
<p>My question is about <a href="https://github.com/MIR-MU/CompuBERT" rel="nofollow noreferrer">CompuBERT</a> (<a href="http://ceur-ws.org/Vol-2696/paper_235.pdf" rel="nofollow noreferrer">Three is Better than One Ensembling Math Information Retrieval Systems</a>)</p> <p>It is written on page 21 (table 2):</p> <blockqu...
333
tokenization
How to model an inverse &quot;token relationship&quot; in Petri nets?
https://cs.stackexchange.com/questions/144380/how-to-model-an-inverse-token-relationship-in-petri-nets
<p>Suppose I have the following Petri net:</p> <p><a href="https://i.sstatic.net/4zyXD.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/4zyXD.png" alt="Petri net" /></a></p> <p>I wonder whether it is possible to model an inverse relationship between <span class="math-container">$p2$</span> and <span class...
<p>Without extra transitions, the best you can do is make them alternate:</p> <p><a href="https://i.sstatic.net/BqQ1u.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/BqQ1u.png" alt="The original net plus two additional arcs" /></a></p> <p>Extra transitions can be used to choose between filling <em>p2</em...
334
tokenization
does a non-terminated string count as a token in c?
https://cs.stackexchange.com/questions/119130/does-a-non-terminated-string-count-as-a-token-in-c
<p>so, I am preparing for an exam which includes lexical analysis from compiler design. I was wondering what is the number of tokens in the following code-</p> <pre><code>int main() { /* comment printf("Hello */ There ");*/ return 0; } </code></pre> <p>so, I am thinking upto first "*/" it will be a multiline co...
<p>No, in C unterminated strings are not tokens. The C language definition precisely describes what a token is; among other things, they include (complete) string literals and a fallback category of "single non-whitespace characters" which are not otherwise matched by the lexical grammar. This latter category does not,...
335
tokenization
Token-sliding as a kind of Petri net: well-studied subclass?
https://cs.stackexchange.com/questions/71854/token-sliding-as-a-kind-of-petri-net-well-studied-subclass
<p>Let a directed graph $G = (V, E)$ be given, plus a constraint map $c: E \rightarrow V$ and a set $T \subseteq V$ of initial token locations. A valid move consists of sliding a token from $v$ to $w$ if:</p> <ul> <li>$v \in T$ — to slide a token, it must be there.</li> <li>$w \not \in T$ — to move a token somewhere,...
336
tokenization
What token does a &quot;peek&quot; operation refer to in Lexical analysis?
https://cs.stackexchange.com/questions/90908/what-token-does-a-peek-operation-refer-to-in-lexical-analysis
<p>Given a grammar for a space delimited list of words:</p> <pre><code>S -&gt; word { space word } word -&gt; [a-zA-Z]+ space -&gt; [ \t]+ </code></pre> <p>And given the input "Hello World", what token would a <code>peek()</code> operation return?</p>
337
tokenization
What data is stored in the symbol table for a number token?
https://cs.stackexchange.com/questions/21560/what-data-is-stored-in-the-symbol-table-for-a-number-token
<p>I'm reading the Dragon Book. The following is from the start of Section 3.1.3.</p> <blockquote> <p>When more than one lexeme can match a pattern, the lexical analyzer must provide the subsequent compiler phases additional information about the particular lexeme that matched. For example, the pattern for token <st...
<p>Typical lexers will return a sequence of pairs, where the pair consists of the token type and an optional value. For a token such as <code>12345</code>, the token type will be something like "number" and the value will be 12345. If the lexer only emitted the information that there was a numeric constant in the inpu...
338
tokenization
How does the token method of amortized analysis work in this example?
https://cs.stackexchange.com/questions/62484/how-does-the-token-method-of-amortized-analysis-work-in-this-example
<p>Below is the description of the answer to a question which says the following:</p> <p><strong>Design a data structure to support two operations for a dynamic multiset S of integers which allows duplicate values.</strong></p> <ol> <li>Insert operation for one element </li> <li>Delete-larger-half(S) deletes the ...
<blockquote> <p>Then how does deleting with median work unless there is n comparisons (i.e compare each element with the median).</p> </blockquote> <p>After partitioning (they assume Quicksort-style partitioning), you can just delete the last $\lceil |S| / 2 \rceil$ elements in the array.</p> <blockquote> <p>Amor...
339
tokenization
How to do high performance string matching when comparing unordered sets of tokens
https://cs.stackexchange.com/questions/37174/how-to-do-high-performance-string-matching-when-comparing-unordered-sets-of-toke
<p>This is the problem:</p> <p>I have some strings stored in the database. Each of the strings can be seen as a set of tokens separated by comma with no repetition (I mean a token cannot appear more than one time in a string).</p> <p>I want to know if a new string matches any of them without taking token order into a...
<p>So you want to use the <a href="https://en.wikipedia.org/wiki/Jaccard_index" rel="nofollow">Jaccard index</a> as your metric of similarity. Well, the Wikipedia page for the Jaccard index (and which I linked to in the comments above) already has some hints on methods for finding close matches, more efficiently than ...
340
tokenization
Problem identification: splitting string into tokens taken from a given, possible overlapping set
https://cs.stackexchange.com/questions/162382/problem-identification-splitting-string-into-tokens-taken-from-a-given-possibl
<p>I am facing the following problem in a script I am trying to develop:</p> <p>Given a string and a set of tokens, where the tokens are known and are overlapping (the set can contain the tokens 'a', 'b' and 'ab'), I need to split the string into a list of tokens from the set. I know there can be multiple ones, I don't...
<p>This problem is often called the Word Break problem and is a classic exercise for dynamic programming.</p> <p>Base case: if you don't have a string, it's a yes instance.</p> <p>For every token, if the string ends with that token, remove that suffix from the string and recurse.</p> <p>If no token is a suffix of your ...
341
tokenization
In NLP, does the lexer have to tag the tokens before the parser?
https://cs.stackexchange.com/questions/51146/in-nlp-does-the-lexer-have-to-tag-the-tokens-before-the-parser
<p>In NLP, does the lexer have to tag the tokens before the parser?</p> <p>I.e. does the lexer have to classify the tokens to morphological categories before the parser?</p> <p>I'm thinking yes, but is this also the only way to do the parsing?</p>
<p>Morphological tags can help the parser. On the other hand, the complete sentence structure, maybe even the paragraph context may help to finally disambiguate possible tags for a token. So there is no yes/no answer. Except, maybe, that tagging is, afaik, usually not attributed as work of the lexer, but rather a modul...
342
tokenization
What determines the number of arcs and tokens in a Petri Nets Model?
https://cs.stackexchange.com/questions/54444/what-determines-the-number-of-arcs-and-tokens-in-a-petri-nets-model
<p>I study Petri Nets to model some cases related to my job. Currently, I study the basics of Petri Nets and am confused. (For the time being I couldn't get a textbook yet, I will.)</p> <p><strong>my questions</strong></p> <p><strong>q1)</strong> What determines the required number of tokens required for the model to...
<p>One of the resources I used to teach myself about Petri Nets was the chapters on Petri Nets in the textbook “Petri Nets and Grafcet: Tools for Modeling Discrete-Event Systems” (David and Alla, 1992).</p> <p>An example process and a Petri Net model of the process may help you answer your first two questions (Chenier...
343
tokenization
What does it mean to have tokens at a state in a balancing network?
https://cs.stackexchange.com/questions/49827/what-does-it-mean-to-have-tokens-at-a-state-in-a-balancing-network
<p>I was assigned as homework:</p> <blockquote> <p>Suppose we have a width-w balancing network of depth $d$ in a quiescent state $s$ called $B$. Let $n = 2^d$. Prove that if n tokens enter the network on the same wire, pass through the network, and exit, then $B$ will have the same state after the tokens exit ...
<p>It seems that there is a typo – $s$ and $B$ represent the same thing. It looks like the original $s$ was changed to $B$, but the author of the question forgot to delete $s$.</p> <p>The state of the network is the state of all balancers – which way they point.</p>
344
tokenization
Why do we not use CFGs to describe the structure of lexical tokens?
https://cs.stackexchange.com/questions/55567/why-do-we-not-use-cfgs-to-describe-the-structure-of-lexical-tokens
<p>This was an exam question for my course and I am struggling to actually answer it in a way that is not fluff.</p> <p>Here is my current answer:</p> <p><em>CFGs describe how non-terminal symbols are converted into terminal symbols via a parser. However, a scanner defines what those terminal symbols convert to in te...
<p>You don't use CFGs because typically lexical analysis can be performed using regular automata, and these are faster than context-free parsers. It's a question of efficiency.</p>
345
tokenization
Why doesn&#39;t the C/C++ compilers create different tokens for different types of numbers?
https://cs.stackexchange.com/questions/89856/why-doesnt-the-c-c-compilers-create-different-tokens-for-different-types-of-n
<p>Based on <a href="https://www.geeksforgeeks.org/cc-tokens/" rel="nofollow noreferrer">GeeksforGeeks</a> and many other sites, the C/C++ compilers will create the same token for <code>float</code>/<code>int</code> etc.</p> <p>However if we have something like this:</p> <pre><code>int A[10.5]; </code></pre> <p>then...
<p>This is in fact an implementation detail of the compiler. The page you referenced only shows one way of assigning types to tokens, while there are also others. The compiler <strong>could</strong> have a lexer that distinguishes between integral and non-integral constants and the parser then cannot match the declarat...
346
tokenization
Why is particular token missing in LALR lookahead set?
https://cs.stackexchange.com/questions/59548/why-is-particular-token-missing-in-lalr-lookahead-set
<p>I ran the following grammar (pulled from the dragon book) in the Java Cup Eclipse plugin:</p> <pre><code>S' ::= S S ::= L = R | R L ::= * R | id R ::= L </code></pre> <p>The items associated with state 0 given in the Automaton View are as follows:</p> <pre><code>S ::= ⋅S, EOF S ::= ⋅L = R, EOF S ::= ⋅R, EOF L ::=...
<p>In state 0, <code>R ::= ⋅L</code> can only be generated by <code>S ::= ⋅R</code>. In <code>L ::= ⋅* R</code>, the dot precedes <code>*</code>, not <code>R</code>, so no further items are generated by it.</p> <p>The dragon book uses this grammar as an example of the inadequacy of SLR, and the correct computation of ...
347
tokenization
How to Determine Places, Transitions and Tokens in a Scenario when Modeling with Petri Nets?
https://cs.stackexchange.com/questions/71814/how-to-determine-places-transitions-and-tokens-in-a-scenario-when-modeling-with
<p>When modeling a scenario with Petri nets how should I determine the places, transitions and tokens?</p> <p><strong>Example:</strong> </p> <p>There are two exam assistants in an exam hall observing the exam. They stand in front of the exam hall. When a student has a question one of the assistants goes to him and an...
<p>If you find it challenging to apply Petri Nets in modeling an application then it may help to consider the following mapping between the types of words found in a text description of an application with the types of Petri Net elements found in a Petri Net diagram of the application:</p> <ol> <li>Nouns are candidate...
348
tokenization
How to Tokenize a String and Save Each Substring Separately (Independent Names for Each Substring)
https://cs.stackexchange.com/questions/119887/how-to-tokenize-a-string-and-save-each-substring-separately-independent-names-f
<p>I am given two files one with the name of person and the location that they are from (Evan Lloyd|Brownsville) and one with the name and salary (Evan Lloyd|58697) (the line number that you find the employee on in the first file is not necessarily the line number that find the employee on in the second). The user inpu...
349
tokenization
Expected gain of a game of chance with differently-priced tokens
https://cs.stackexchange.com/questions/4899/expected-gain-of-a-game-of-chance-with-differently-priced-tokens
<p>Foo and Bar are playing a game of strategy. At the start of the game, there are $N$ apples, placed in a row (in straight line). The apples are numbered from $1$ to $N$. Each apple has a particular price value.</p> <p>The price of $i$th apple is $p_i$.</p> <p>In this game, the players Foo and bar make an alternativ...
<p>They probably meant you to solve it using dynamic programming. Since I'm guessing this is an exercise, I won't say anything more on this front. Also, your program is incorrect: it doesn't consider the actions of Bar.</p> <p>The game is not really a <em>game of strategy</em>, since there are no choices involved, onl...
350
tokenization
In NLP, tokens not seen in training sample, but you know or don&#39;t know what they are
https://cs.stackexchange.com/questions/22092/in-nlp-tokens-not-seen-in-training-sample-but-you-know-or-dont-know-what-they
<p>In NLP, do you distinguish tokens that you don't observe in a training sample and still expect that they may occur in a test sample, between </p> <ul> <li>those you know what they are, and</li> <li>those you don't know what they are, and how many of them</li> </ul> <p>If yes, how do you treat them differently to e...
351
tokenization
Relation between programming languages requiring declaration of variables before use and using the token class $\text{id}$ while parsing
https://cs.stackexchange.com/questions/136591/relation-between-programming-languages-requiring-declaration-of-variables-before
<p>I was going through the text <em>Compilers: Principles, Techniques and Tools</em> by <em>Ullman et. al.</em> where I came across the following excerpt.</p> <blockquote> <p>Example 4.11. Consider the abstract language <span class="math-container">$L_1 = \text{ { $wcw$ | $w$ is in $(a|b)^*$}}$</span>. <span class="mat...
<p>In order for their example to work, the authors need identifiers to be of unlimited length. This is because the language <span class="math-container">$$ \{ wcw : w \in \{a,b\}^*, |w| \leq n \} $$</span> is context-free (indeed, regular).</p> <p>The syntax of a language like Pascal or Algol is context-free. This acco...
352
tokenization
Why do we need $k \geq n$ in Dijkstra&#39;s token ring self-stabilizing system?
https://cs.stackexchange.com/questions/49738/why-do-we-need-k-geq-n-in-dijkstras-token-ring-self-stabilizing-system
<p>Let's say I have a ring with four nodes $n=\{0;1;2;3\}$ and three possible states $k=\{0;1;2\}$. A transient failure happens and the system ends up in an illegal state.</p> <p>I know from the restriction ($k \geq n$) that there should be an execution that makes the system stay in its illegal state indefinitely, but...
<p>Here is how to solve this for your particular $n$ and $k$. Your system has $k^n = 81$ possible states. You can describe the evolution of the system as a directed graph: there is an edge $s_1 \to s_2$ if state $s_1$ evolves in one step to state $s_2$. Some states are legal, some are illegal. You want to find an illeg...
353
tokenization
On a table there are $N$ stacks. Stack $i$ contains $i$ tokens. Minimum number of moves to make all stacks empty
https://cs.stackexchange.com/questions/162707/on-a-table-there-are-n-stacks-stack-i-contains-i-tokens-minimum-number-o
<p>On a table there are <span class="math-container">$n$</span> stacks (numbered <span class="math-container">$1$</span> to <span class="math-container">$n$</span>). Stack <span class="math-container">$i$</span> contains <span class="math-container">$i$</span> tokens (<span class="math-container">$1 \leq i \leq n$</spa...
<p>Let <span class="math-container">$k$</span> be the smallest integer such that <span class="math-container">$2^k &gt; n$</span>.</p> <p><strong>Any solution must use at least <span class="math-container">$k$</span> moves</strong></p> <p>Suppose towards a contradiction that there exists a winning strategy using at mos...
354
tokenization
How to define at least one occurrence of a string between two tokens in bottom up LALR(1) parser grammar
https://cs.stackexchange.com/questions/9961/how-to-define-at-least-one-occurrence-of-a-string-between-two-tokens-in-bottom-u
<p>I am trying to define a non terminal symbol in a LALR(1) grammar (with CUP parser). It is requested that </p> <pre><code>the &lt;code&gt; token must appear exactly twice, while the &lt;hour&gt; token must appear at least once. </code></pre> <p>In the end I came up with this definition:</p> <pre><code>section ...
<p>My solution, suggested by a friend, is to use a Finite State Machine. I drew a Deterministic Finite Automata, and $C$ is the final state accepted by this machine:</p> <p><img src="https://i.sstatic.net/iTtsg.png" alt="DFA"></p> <p>I then transformed it into a right regular grammar:</p> <pre><code>section ::= ...
355
tokenization
In describing the tokens of a programming language using RE, it is not necessary to have the $\epsilon$ or t. Why is this?
https://cs.stackexchange.com/questions/85615/in-describing-the-tokens-of-a-programming-language-using-re-it-is-not-necessary
<p>In describing the tokens of a programming language using regular expressions, it is not necessary to have the $\epsilon$, (for the empty set) or t (for the empty string). Why is this?</p> <p>Please tell me why is it not necessary thanks</p>
<p>The empty set is only needed in order to describe the empty regular language. The set of tokens of a certain type is never empty, so the empty set isn't needed.</p> <p>Similarly, the empty string is only needed to describe regular languages which include the empty string. The empty string isn't a token of any type,...
356
tokenization
Searching through all program of a stack-based language with little memory
https://cs.stackexchange.com/questions/53506/searching-through-all-program-of-a-stack-based-language-with-little-memory
<p>I wrote a simple stack based language, and am looking to exhaustively generate all programs for it, to find the shortest program that generates a particular output.</p> <p>Given a program fragment, I can determine if it is terminated, and, if not, how many operands it requires. Currently, my search strategy is:</p...
<p>I can give you a answer about the principles.</p> <p>Find a computable bijection between your programs and the natural numbers. In essence, you would construct an <a href="https://en.wikipedia.org/wiki/Admissible_numbering" rel="nofollow">admissible numbering</a>. Then, you can just iterate over the naturals, compu...
357
tokenization
Why does lexer has O(n) time complexity?
https://cs.stackexchange.com/questions/162979/why-does-lexer-has-on-time-complexity
<p>According to my CS knowledge so far, a lexer uses DFA(which takes linear time) for 'each' token type to find the next token, so in the worst case, it should try 'all possible' token types of a language. (there are some other reasons, e.g. to find the 'longest matching pattern' to distinguish between <code>if a</code...
<p>Your assumption of execution is based on a suboptimal backtracking implementation based on NFA needing to explore all paths through it.</p> <p>A more optimal implementation would have a bitset of all states the NFA is in simultaneously. This lets you represent all <span class="math-container">$2^n$</span> states wit...
358
tokenization
What time complexity (big o) is this specific web crawler implementation?
https://cs.stackexchange.com/questions/104708/what-time-complexity-big-o-is-this-specific-web-crawler-implementation
<blockquote> <p>Note: this question was marked as a duplicate in favor of <a href="https://cs.stackexchange.com/questions/23593/is-there-a-system-behind-the-magic-of-algorithm-analysis">this question/answer</a> which attempts to provide a generic formula for translating code to mathematics. </p> <p>Unfortunately...
<p>I didn't read your code, but based on the overview, the running time to handle a list of size <span class="math-container">$n$</span> containing nested sublists of size <span class="math-container">$x$</span> appears to be <span class="math-container">$O(nx)$</span>. Here's my reasoning:</p> <ul> <li><p>You iterat...
359
tokenization
a problem with Suzuki-Kasami mutex algorithm
https://cs.stackexchange.com/questions/89680/a-problem-with-suzuki-kasami-mutex-algorithm
<p>I have some sort of a misunderstanding regarding the Suzuki-Kasami distributed mutex algorithm. I am writing my question here because I failed on gaining access to the original paper.</p> <p>I will call processes sites for convenience.</p> <p><strong>My question</strong></p> <p>Why the token holder $j$ releases i...
360
tokenization
Prove maximum score is achieved by being greedy
https://cs.stackexchange.com/questions/167438/prove-maximum-score-is-achieved-by-being-greedy
<p>I have a list of tokens <code>T</code>, of length <code>n</code>. Initially I have power <code>p</code> and a <code>score</code> of zero. In one move, I can play <em>any</em> token <code>t</code> either face up or face down.</p> <p>(a) I can play <code>t</code> face up, provided that I have at least as much power as...
<p>If <span class="math-container">$(t_0, t_1, …, t_{n-1})$</span> is the sequence of tokens played by your greedy solution, you can show by induction that for any <span class="math-container">$k\in \{0, …, n\}$</span>, the sequence <span class="math-container">$(t_0, …, t_{k-1})$</span> is the sequence of <span class=...
361
tokenization
Is there a way to connect a deep language model output to input?
https://cs.stackexchange.com/questions/115948/is-there-a-way-to-connect-a-deep-language-model-output-to-input
<p>In models like GPT-2, TXL and Grover, is there a good way to know which input weights (tokens) resulted in each token of the output? </p>
362
tokenization
An Arithmetic Encoding&#39;s length being ambiguous?
https://cs.stackexchange.com/questions/157561/an-arithmetic-encodings-length-being-ambiguous
<p>Say they are two tokens, A and B. A has probability weight 0.99 (and B has 0.01). If I want to encode the sequence &quot;AAA&quot;, wouldn't the binary encoding just be &quot;0&quot;? And wouldn't that be the same for encoding &quot;AA&quot;, or &quot;AAAA&quot;, or an number of A's? How is the decoder supposed to k...
<p>Yes - you need to be able to indicate EOF (and EOF is obviously meaningful). Note that you may have a natural message boundary in terms of decoding; for example, if you are writing to a file then the file has a size and thus the information can be encoded in a trailer (e.g., how many of the final decoded characters ...
363
tokenization
How to prove LastToken problem is NP-complete
https://cs.stackexchange.com/questions/118196/how-to-prove-lasttoken-problem-is-np-complete
<p>Consider the following game played on a graph <span class="math-container">$G$</span> where each node can hold an arbitrary number of tokens. A move consists of removing two tokens from one node (that has at least two tokens) and adding one token to some neighboring node. The LastToken problem asks whether, given a ...
<p>Here is a reduction from the Hamiltonian path. Given a graph <span class="math-container">$G=(V,E)$</span>. Add a vertex <span class="math-container">$v_0$</span> to the graph and connect it to all vertices in the graph. Set <span class="math-container">$t(v_0)=2$</span>. Set <span class="math-container">$t(u) = 1$<...
364
tokenization
The maximum &amp; minimum number of sources that can be multiplexed so that no data loss occurs on TOKEN BUCKET?
https://cs.stackexchange.com/questions/35841/the-maximum-minimum-number-of-sources-that-can-be-multiplexed-so-that-no-data
<p>A link of capacity 100 Mbps is carrying traffic from a number of sources. Each source generates an on-off traffic stream; when the source is on, the rate of traffic is 10 Mbps, and when the source is off, the rate of traffic is zero. The duty cycle, which is the ratio of on-time to off-time, is 1 : 2. When there is ...
<p>A is correct. As 1st part is already described i will explain derivation of S2 only. Let there are N stations. When buffer is added then even when source is off, data will persist so considering duty cycle here we say that the actual data transmitted per unit time during the whole duty cycle is (1/3)<em>10 [for 1 u...
365
tokenization
Closure of regular languages under &quot;inverse second half&quot;
https://cs.stackexchange.com/questions/116780/closure-of-regular-languages-under-inverse-second-half
<blockquote> <p><strong>Theorem.</strong> Show that if <span class="math-container">$L$</span> is regular, then so is <span class="math-container">$$ \varphi(L)=\left\{w \in \Sigma^{*} \mid \text {there exists an } \alpha \in \Sigma^{*} \text { with }|\alpha|=|w| \text { and } \alpha w \in L\right\} $$</span> <s...
<p>The white token is a placeholder, which just remembers the original state of the red token (of course, the roles of these two tokens can be switched); this is the state at which <span class="math-container">$M$</span> would be after reading <span class="math-container">$\alpha$</span>. The red token corresponds to t...
366
tokenization
How to generate, validate, and invalidate a set/list of numbers in O(1) time and space?
https://cs.stackexchange.com/questions/124714/how-to-generate-validate-and-invalidate-a-set-list-of-numbers-in-o1-time-and
<p>Imagine my server is generating &quot;tokens&quot; of some sort for a client on a regular basis. When a client asks for a token, the server responds with a new value (and any other supplemental information it wants to, like a &quot;witness&quot;). Later, the client will submit the token (and optionally the witness) ...
<p>The easiest and secure method of doing this is making a token an (id, signature) pair where you randomly generate a fixed-size id (e.g. 128-bits) using a method that avoids collisions (a hash of a counter plus timestamp plus system RNG works in almost every scenario). The signature could then be something as simple ...
367
tokenization
Optimal common substring elimination
https://cs.stackexchange.com/questions/170609/optimal-common-substring-elimination
<p>The problem:</p> <p>You are given a list of strings as an input. You may perform any number of &quot;token substitution&quot; operations. A token substitution is performed by: removing any substring, replacing instances with a new token, and adding that substring to the list. The output of the algorithm is the modif...
368
tokenization
Is ambiguity in Programming Language always bad?
https://cs.stackexchange.com/questions/105388/is-ambiguity-in-programming-language-always-bad
<p>Suppose we have a token and our language allows the compiler to build two different derivation trees. However, it can happen that there exists two semantic ways to interpret our token. So ambiguity is not a problem in this case. Am I correct?</p>
<p>There are computer languages with ambiguous grammars. They decide how to compile ambiguous code by applying rules outside the grammar (there’s no law saying that a language has to be defined by a grammar only). The problem is if your <em>language</em> is ambiguous - if you have code and cannot tell how it should be ...
369
tokenization
Can Earley parser work in parallel?
https://cs.stackexchange.com/questions/110403/can-earley-parser-work-in-parallel
<p>Since Earley parser finds all possible application variants for a token, can it parse text in parallel, unlike the usual parser like stack-based, etc. You just need to modify the start of each parallel chunk of tokens, then while going backwards while constructing a table you combine and validate the found rules li...
<p>Yes, Earley's algorithm can be parallelized, but not in the way that you are thinking.</p> <h1>Earley's Algorithm In Particular</h1> <p>You ask about Earley's algorithm <em>specifically</em>. Parallelizing the algorithm in the way you suggest is unlikely to be faster. Researchers Peter Ahrens, John Feser, and Robin ...
370
tokenization
How do balancers work in the context of counting and balancing networks?
https://cs.stackexchange.com/questions/49829/how-do-balancers-work-in-the-context-of-counting-and-balancing-networks
<p>I was learning what balancing networks are and at some point the art of multicore programming talks about balancers. The text book says:</p> <blockquote> <p>A balancer is a simple switch with two input wires and two output wires, called the top and bottom wires (or sometimes the north and south wires). Tokens...
<p>The book is correct, and the algorithm works as advertised. See if you can prove that it does. No explicit counting is necessary.</p> <p>A very similar situation occurs for languages: $(ab)^*(a + \epsilon)$ is regular even though $\{ w : 0 \leq \#_a(w) - \#_b(w) \leq 1 \}$ is not.</p>
371
tokenization
Is there an alternative for the formal language theory that could be used for flowchart diagrams?
https://cs.stackexchange.com/questions/151514/is-there-an-alternative-for-the-formal-language-theory-that-could-be-used-for-fl
<p>I am creating a tool for validating, parsing, and interpreting flowchart diagrams on <a href="https://diagrams.net" rel="nofollow noreferrer">diagrams.net</a>, and it is necessary to give users an opportunity to define a set of rules for the diagram. So, in the end, I want to achieve something like ANTLR for diagram...
<p>Not sure why you would go straight to CFGs to resolve this when most flowcharts can be represented as rational functions, AKA finite-state transducers. These can be expressed as regular patterns: considering your diagram, it would be expressed as</p> <pre><code>((1, print['1']) | (2, print['2']) | (3, end)) </code><...
372
tokenization
Why separate lexing and parsing?
https://cs.stackexchange.com/questions/39898/why-separate-lexing-and-parsing
<p>It's possible to parse a document using a single pass from a state machine. What is the benefit of having two passes, ie. having a lexer to convert text to tokens, and having a parser to test production rules on those tokens? Why not have a single pass that applies production rules directly to the text?</p>
<p>You don't have to separate them. People combine them into <a href="https://en.wikipedia.org/wiki/Scannerless_parsing">scannerless parsers</a>. </p> <p>The key disadvantage of scannerless parsers appears to be that the resulting grammars are rather complicated -- more complicated than the corresponding combination o...
373
tokenization
Parsing algorithms
https://cs.stackexchange.com/questions/159342/parsing-algorithms
<p>I have experimented with a grammar that I could turn into a strict left-to-right finite state automaton driven algorithm (bottom up, table driven). The FSA could be complex, that's not a problem. It doesn't need to deal with infinite recursive structures.</p> <p>I then moved the grammar into BNF, and built a standar...
<p>The comment basically has the right idea: What you want is some kind of LR parsing; GLR is fine.</p> <p>You see, if a grammar is not <span class="math-container">$LR(k)$</span> but you try to build a <span class="math-container">$LR(k)$</span> automaton for it anyway, then the automaton is still correct and will sti...
374
tokenization
Finding a Simple Distribution In a Binary String
https://cs.stackexchange.com/questions/68693/finding-a-simple-distribution-in-a-binary-string
<p>Unsupervised feature discovery of text that started with its bit string representation would need to discover octets were the first-order parse of such a bit string. This raises a question:</p> <p>What is the technique called that can discover that a binary string, for example:</p> <p><code>0100100111110010101010...
<p>TL;DR: use maximum likelihood and discrete optimization.</p> <h2>Evaluating candidate models: the maximum likelihood principle</h2> <p>If you have a candidate model, you can evaluate how well it fits the data using the maximum likelihood principle.</p> <p>If $M$ is a model and $x$ is a string, let $P(x|M)$ denote...
375
tokenization
What defines how many lookahead a lexer has?
https://cs.stackexchange.com/questions/141300/what-defines-how-many-lookahead-a-lexer-has
<p>if a lexical grammar has multiple token which start with the same character like <code>&gt;</code> <code>&gt;&gt;</code> <code>&gt;&gt;=</code> and their longest length is 3, does it have 2 character lookahead?</p> <p>Or is it implementation defined. Does the number of character required to produce a fixed size toke...
<p>&quot;Lookahead&quot; is an aspect of a particular parsing algorithm, and might be different for different parsing algorithms using the same grammar. You can't talk about lookahead without specifying which parsing algorithm is in use.</p> <p>If you are using a top-down LL parsing algorithm, parsing decisions need to...
376
tokenization
Strategy to designing grammar for a LR(1) parser
https://cs.stackexchange.com/questions/152048/strategy-to-designing-grammar-for-a-lr1-parser
<p>Is it better to think about tokens from right to left and perform right factoring on grammar for an LR(1) parser? As apposed to thinking about tokens left to right and doing left factoring on grammar for an LL(1) parser.</p> <p>Example java import statement.</p> <pre><code>S1 -&gt; S2 ; S2 -&gt; S5 S2 -&gt; S3 id S2...
<p>It's not difficult to write grammars, and particularly not LR(k) grammars (despite all the claims to the contrary you'll find floating around). You should start by trying to write down a simple description of the language which reflects the actual structure of the language. In many cases, that's easier to do if you ...
377
tokenization
Find all substrings that fit the mask with asterisks
https://cs.stackexchange.com/questions/91603/find-all-substrings-that-fit-the-mask-with-asterisks
<p>There is a problem.</p> <p>Given string $text$ containing only letters and string $mask$ containing letters and asterisks (*), where asterisk means substitution of zero or more letters, find all substrings of $text$ that fit $mask$.</p> <p>There is an example: let's $text=cabccbacbacab$, $mask=\textbf{ab}*\textbf{...
<p>Yes, there is a more efficient algorithm. Your algorithm can take exponential time.</p> <p>You can check whether there exists any match in $O(nm)$ time, where $n$ is the length of text and $m$ is the length of mask, and find all matches in $O(n^2m)$ time. I'll show two solutions, one using dynamic programming and...
378
tokenization
How to find the size of gaps between entries in an array so that the first and last element of the array is filled and the gaps are all of equal size?
https://cs.stackexchange.com/questions/127994/how-to-find-the-size-of-gaps-between-entries-in-an-array-so-that-the-first-and-l
<p>I have an array a of n entries. I need to place a token on the first and last position of that array, so <code>a[0] = 1</code> and <code>a[n-1] = 1</code>.</p> <p>I now want to place additional tokens into that array with a distance inbetween each index i where <code>a[i] = 1</code> that is greater than 2 (so placin...
<p>If I understand your problem correctly, the tokens (lanterns) can be placed every <span class="math-container">$x$</span> blocks (starting from <span class="math-container">$0$</span>) if and only if <span class="math-container">$x&gt;2$</span> is a divisor of <span class="math-container">$n-1$</span>.</p> <p>For e...
379
tokenization
Has there been a lexer that takes in much more than a regular language?
https://cs.stackexchange.com/questions/19837/has-there-been-a-lexer-that-takes-in-much-more-than-a-regular-language
<p>I understand the restrictions, because a regular language is expressive enough to allow all types of tokens. And even if some context is needed in many languages to tokenize properly, they all seem to be "approximately" regular languages.</p> <p>Yet I would be interested if any attempt in any programming language, ...
<ul> <li><p>FORTRAN is famous for having some difficult to lex constructs, but those difficulties result probably more from having been designed before the classification was established (or at least known in programming circles)</p></li> <li><p>Several languages are described with a type-3 lexers but some characterist...
380
tokenization
Lexical analysis
https://cs.stackexchange.com/questions/82874/lexical-analysis
<p>Will the below statement cause any lexical error ?</p> <pre><code>int a123c ; </code></pre> <p>According to me, int would be tokenized as a keyword and there would be a lexical error when "a123c" would be encountered as it doesn't fall into any token category.</p> <p>I read this question <a href="https://stackove...
<p>Which error message is generated by which phase of a compiler depends on the underlying language structure and implementation of the compiler. </p> <p>Nevertheless, lexical analyzer is responsible for generating tokens, so at this phase you could check if some lexeme/token is valid or not. For example, you could ch...
381
tokenization
What information do we get from a compiler&#39;s parse tree?
https://cs.stackexchange.com/questions/16647/what-information-do-we-get-from-a-compilers-parse-tree
<p>In the <a href="https://class.coursera.org/compilers/lecture/index" rel="nofollow">compiler course by Alex Aiken on Coursera</a>, more specifically lecture <a href="https://class.coursera.org/compilers/lecture/20" rel="nofollow">05-02 Context Free Grammars</a>, the professor says that CFGs give answers of the type ...
<p><strong>Prelude</strong> It might be useful to be pedantic and start with a surprising fact: compilers do <strong>not</strong> use context free grammars, contrary to what you've been told. Instead they use something closely related but subtly different, which might be termed <strong>context-free transducers</stron...
382
tokenization
Efficient algorithm for iterated find/replace
https://cs.stackexchange.com/questions/28307/efficient-algorithm-for-iterated-find-replace
<p>I'm looking for an algorithm for doing iterated find/replace, where the act of finding the replacement list of tokens for a given find is slow.</p> <p>Specifically: I have a function, <code>f</code>, that maps a sequence of tokens to either a shorter list of tokens or None. However, it is slow.</p> <p>I want to re...
<p>This isn't a complete answer, but it provides some context that's far too long to put in a comment. What you've described is an instance of a <a href="http://en.wikipedia.org/wiki/Semi-Thue_system" rel="nofollow noreferrer">string rewriting system</a>, also known as a semi-Thue system. Start with a finite alphabet, ...
383
tokenization
Modelling a dependency of multiple transitions on data in one place
https://cs.stackexchange.com/questions/57900/modelling-a-dependency-of-multiple-transitions-on-data-in-one-place
<p>We are modeling our process using a colored Petri net. One of the limitations we have is that when multiple transitions depend on one place, only one of those transitions will fire because then the token and data is consumed.</p> <p>How can we model our process, or what type of Petri net property can we use, to fac...
<p>I do not know if there is a variant of Petri nets that captures your intent exactly -- there <em>probably</em> is, there are so many -- but the feature can be expressed with regular Petri nets.</p> <p>Just add a transition that creates tokens in multiple places, one per original transitions. Then, all three follow-...
384
tokenization
How to implement a maximal munch lexical analyzer by simulating NFA or running DFA?
https://cs.stackexchange.com/questions/97374/how-to-implement-a-maximal-munch-lexical-analyzer-by-simulating-nfa-or-running-d
<p>I'm planning to implement a lexical analyzer by either simulating NFA or running DFA using the input text. The trouble is, the input may arrive in small chunks and the memory may not be enough to hold one very long token in the memory.</p> <p>Let's assume I have three tokens, "ab", "abcd" and "abce". The NFA I obta...
<p>There are two ways to handle this issue:</p> <ol> <li><p>The most common implementation (the one used in lex, flex and other similar scanner generators) is to always recall the last accept position and state (or accept code). When no more transitions are possible, the input is backed up to the last accept position ...
385
tokenization
Why is $O(nk)$ an upper bound for the $k$-gossip problem?
https://cs.stackexchange.com/questions/146390/why-is-onk-an-upper-bound-for-the-k-gossip-problem
<p>I am studying the <span class="math-container">$k$</span>-gossip problem on dynamic graphs against an adaptive adversary. Essentially, we are given a set of tokens <span class="math-container">$\mathcal{T}$</span> which are distributed amongst the nodes such that each token is distributed to at least one node. Impor...
386
tokenization
Why is using a lexer/parser on binary data so wrong?
https://cs.stackexchange.com/questions/899/why-is-using-a-lexer-parser-on-binary-data-so-wrong
<p>I often work with <a href="http://en.wikipedia.org/wiki/Lexical_analysis">lexer</a>/<a href="http://en.wikipedia.org/wiki/Parsing">parsers</a>, as opposed to a parser combinator and see people who never took a class in parsing, ask about parsing binary data. Typically the data is not only binary but also context sen...
<p>In principle, there is nothing wrong.</p> <p>In practice,</p> <ul> <li><p>most non-textual data formats I know are not context-free and are therefore not suitable for common parser generators. The most common reason is that they have length fields giving the number of times a production has to be present.</p> <p>...
387
tokenization
ir - Using document-term [Boolean] incidence matrix for answering a query
https://cs.stackexchange.com/questions/161459/ir-using-document-term-boolean-incidence-matrix-for-answering-a-query
<p>The book &quot;Introduction to Information Retrieval&quot; <a href="https://nlp.stanford.edu/IR-book/html/htmledition/an-example-information-retrieval-problem-1.html" rel="nofollow noreferrer">talks about term-document incidence matrix</a> for retrieving documents that contain/not-contain certain tokens drawn from a...
388
tokenization
Question about word embeddings in a specific language model - GPT-2
https://cs.stackexchange.com/questions/116184/question-about-word-embeddings-in-a-specific-language-model-gpt-2
<p>How were the <a href="https://openai.com/blog/better-language-models/" rel="nofollow noreferrer">GPT-2</a> token embeddings constructed? </p> <p>The authors mention that they used Byte Pair Encoding to construct their vocabulary. But BPE is a compression algorithm that returns a list of subword tokens that would be...
389
tokenization
Context free grammar for nested arrays separated by commas
https://cs.stackexchange.com/questions/55563/context-free-grammar-for-nested-arrays-separated-by-commas
<p>I have to define a context free grammar for the following rules:</p> <p>(i) A pair of square bracket tokens [] surrounding zero or more values separated by commas. (ii) A value can be another array or a number.</p> <p>A number is represented by the token <code>NUMBER</code>. So for example, <code>[NUMBER, [NUMBER,...
<p>your current implementation doesn't enforce the first condition "A pair of square bracket tokens [] surrounding zero or more values separated by commas" as an empty string or a NUMBER on its own would be accepted by the grammar.</p> <p>You could use the following CFG to maintain the integrity of the constraints</p>...
390
tokenization
To remove all comments in a JavaScript file, do we need just a scanner or also a parser?
https://cs.stackexchange.com/questions/120126/to-remove-all-comments-in-a-javascript-file-do-we-need-just-a-scanner-or-also-a
<p>I was asked how to remove comments in a JavaScript program, but once I gave the regular expression solution, I was asked what if there are comments like text inside of a string:</p> <pre><code>let hi = " // here "; let foo = " use this: /* "; let foo2 = " \" and that */ "; </code></pre> <p>it also can get ...
<p>Scanner (tokenizer) will be enough. Most programming languages do not allow comment nesting, so there is no need to use recursive parsers to strip all comments.</p>
391
tokenization
Why is the step property in a balancing network defined as it is?
https://cs.stackexchange.com/questions/49715/why-is-the-step-property-in-a-balancing-network-defined-as-it-is
<p>I was trying to understand why the equation $y_i = \left( \frac{n}{w} \right) + (i \pmod w) $ describes the step property in a balancing network?</p> <p>First, recall $x_i$ to be the number of tokens a network gets as input and similarly $y_i$ to be the number of output tokens. Recall that a balancing network is ju...
<p>The formula is wrong. The correct formula is $$ y_i = \lfloor \frac{n}{w} \rfloor + [i &lt; (n \mod{w})], $$ where $[C]$ equals 1 if $C$ holds, and 0 otherwise.</p> <p>Sometimes books contain mistakes. When you see a mistake, correct it and proceed. No need to ask us for permission.</p>
392
tokenization
Stuck with shift-reduce conflicts on yacc on grammar to generate palindromic strings on {0,1}
https://cs.stackexchange.com/questions/144332/stuck-with-shift-reduce-conflicts-on-yacc-on-grammar-to-generate-palindromic-str
<p>I have written a yacc program for generating palindromic strings consisting of 0s and 1s. Here is the rules section of the yacc program below:</p> <pre><code>%% program: expr NL { printf(&quot;Valid string.\n&quot;); exit(0); } ; expr: ZERO expr ZERO | ONE expr ONE | ZERO | ONE | ; %% </code></pre> <p>Here <code>Z...
<p>The grammar is, as you say, unambiguous. But it is not <em>deterministic</em>. LR parsers with bounded lookahead can only recognise deterministic languages; since not all unambiguous context-free languages are deterministic, LR parsers cannot recognise all unambiguous context-free languages.</p> <p>Intuitively, pali...
393
tokenization
Item lookaheads versus dot lookaheads for $LR(k)$ with $k \gt 1$?
https://cs.stackexchange.com/questions/48866/item-lookaheads-versus-dot-lookaheads-for-lrk-with-k-gt-1
<p>I was reading "Parsing Techniques: A Practical Guide, Second Edition" by Grune and Jacobs, which details a bunch of different parsing algorithms. In the section on $LR(2)$ parsing, they mention that unlike $LR(1)$ items, which just have an item lookahead (the token that should appear after the completed $LR(1)$ item...
<p>I think you are mistaken, they are needed but the dot look-ahead there is so obvious that you have not paid attention to the fact it is used.</p> <p>First, let's remark that there are three kinds of items:</p> <ol> <li><p>those in which the dot is just before a non-terminal. They never participate in an ambiguous ...
394
tokenization
Variable name starting with integers
https://cs.stackexchange.com/questions/142101/variable-name-starting-with-integers
<p>When I started doing programming I wondered that why the variable names can't start with integer. Back then I accepted that , may be this is how the compiler designers have decided to go with. But now I am studying Compiler Design and they say that lexical analyzer produces token and it is easy / fast if we use iden...
<p>In many languages, <code>1e3</code> is a literal that represents 1000, <code>0x10</code> is a literal that represents 16. If we used your proposed regexp for variable names, it would be ambiguous whether those expressions should be represented as a literal or as a variable name.</p>
395
tokenization
what are all the ways of delimiting blocks
https://cs.stackexchange.com/questions/156268/what-are-all-the-ways-of-delimiting-blocks
<p>To my knowledge, in block-structured programming languages, there are 2, maybe 3 main ways of delimiting a block.</p> <ol> <li>Using start and end tokens, this can be brackets or reserved words etc</li> <li>Using indentation, like python, which uses the offside rule to delimit blocks</li> <li>Using prefix notation f...
396
tokenization
Negotiating a connection between two devices that can&#39;t transmit and receive simultaneously
https://cs.stackexchange.com/questions/153170/negotiating-a-connection-between-two-devices-that-cant-transmit-and-receive-sim
<p>I've got a bit of a puzzle here that sits at the intersection of mathematics and technology. Hopefully this doesn't fall into brainteaser territory - I'm not sure a neat solution is possible!</p> <p>I have two devices. Each one has a short token they would like to share. I am happy for either device to get the other...
397
tokenization
How do stable functions 1 =&gt; 1 relate to Bool?
https://cs.stackexchange.com/questions/29655/how-do-stable-functions-1-1-relate-to-bool
<p>One way to interpret the (simply typed) lambda calculus is via coherence spaces (<a href="http://www.paultaylor.eu/stable/Proofs+Types.html" rel="nofollow">Proofs and Types, chapter 8</a>). For example, we can consider the space containing token element ($\mathbf{1}$) and the space containing two incoherent tokens ...
398
tokenization
Understanding The Mapping Of Edges to Nodes In A Graph Theory Problem
https://cs.stackexchange.com/questions/59995/understanding-the-mapping-of-edges-to-nodes-in-a-graph-theory-problem
<p>I am really confused with this <a href="https://community.topcoder.com/stat?c=problem_statement&amp;pm=13707" rel="nofollow">problem</a>.</p> <p><strong>Here's the problem:</strong> <br></p> <p>You have $N$ points numbered $1$ through $N$,inclusive, and $N$ arrows again numbered $1$ through $N$,inclusive. No two a...
<p>$a[0] = 1, a[1] = 2, a[2] = 3$. </p> <p>"The arrow from place $i$ points to place $a[i−1]$"</p> <p>So yes, all $3$ places in this sample point to themselves. It actually says in the explanation that "in each round each token will stay in the same place". </p> <p>In the second sample, all three places point toward...
399